从现在开始,我使用了优秀的FluentValidation 库来验证我的模型类.在Web应用程序中,我将它与jquery.validate插件结合使用,以执行客户端验证.一个缺点是许多验证逻辑在客户端重复,不再集中在一个地方.
出于这个原因,我正在寻找替代方案.有许多例子出有表示数据注解的使用来执行模型验证.看起来很有希望.我无法找到的一件事是如何验证依赖于另一个属性值的属性.
我们以下面的模型为例:
public class Event
{
[Required]
public DateTime? StartDate { get; set; }
[Required]
public DateTime? EndDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想确保EndDate大于StartDate.我可以编写一个扩展ValidationAttribute的自定义验证属性,以便执行自定义验证逻辑.不幸的是我找不到获取模型实例的方法:
public class CustomValidationAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
// value represents the property value on which this attribute is applied
// but how to obtain the object instance to which this property belongs?
return true;
}
} …Run Code Online (Sandbox Code Playgroud) 我想将所有www流量重定向到非www流量
我已将其复制到我的web.config中
<system.webServer> / <rewrite> / <rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)
根据这篇文章
但我得到了500内部服务器错误.
如果我在db:Product中有这个表,请说
它有过
ProductId
ProductName
ProductType
Run Code Online (Sandbox Code Playgroud)
现在无论出于何种原因我无法命名我的文本框ProductName和ProductType,所以现在我的View方法看起来像这样
public ViewResult Test([Bind(Exclude ="ProductId")] Product)
Run Code Online (Sandbox Code Playgroud)
因此,通过我的游戏,这个产品中没有任何东西可以匹配,因为它们有不同的名称.
所以我猜这是Prefix的用武之地,但我不知道如何使用它.我也不知道如何使用它并同时排除.
有人能举个例子吗?
如何将RadioButtonFor()设置为默认选中
<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
Run Code Online (Sandbox Code Playgroud)
(Html.RadioButton)有出路而不是(Html.RadioButtonFor)
有任何想法吗?
我已经考虑过实现徽章(就像Stack Overflow上的徽章一样)并且认为没有Windows服务会很困难,但我想尽可能避免这种情况.
我想出了一个实施一些例子的计划:
怎么能在数据库中实现呢?或者另一种方式会更好吗?
想要创建自定义数据注释验证.有关如何创建它们的有用指南/示例吗?
首先:
具有最小和最大长度的StringLength.我知道.NET 4可以做到这一点,但是想在.NET 3.5中做同样的事情,如果可能的话,只能定义最小长度(至少x个字符),最大长度(最多x个字符),或者两者都是(在x和y之间).
其次:
使用模数运算验证 - 如果数字是有效长度,我希望使用模数11算法进行验证(我已经在JavaScript中实现了它,所以我想它只是一个简单的移植?)
更新:
解决了第二个问题,只是复制JavaScript实现并进行一些调整,所以不需要解决方案.
当Firefox最近更新到版本6时,我正在努力的网站严重破坏.
浏览到没有哈希标记的任何页面时,网站运行正常,但如果您尝试导航到带有哈希标记的页面(例如#test),或者在应用哈希标记后刷新页面,则页面会无限快速刷新.
这是一个半年前创建的Asp.Net MVC 2网站.
以下内容不会重定向我的页面:这是MVC代码:
[HttpPost]
public ActionResult GoHome()
{
return RedirectToAction("Index", "Home");
}
Run Code Online (Sandbox Code Playgroud)
这是ajax帖子:
$.support.cors = true;
$.ajax({
type: "POST",
url: "http://localhost/UserAccount/GoHome",
dataType: 'json',
crossDomain: true
});
Run Code Online (Sandbox Code Playgroud)
该帖子是成功的,当它与GoHome操作发生冲突时,它不会重定向到Home Controller的Index Action.
美好的一天!
我有这样的方法来获取[DisplayName]属性的属性值(直接附加或使用[MetadataType]属性).我在极少数情况下使用它,我需要进入[DisplayName]控制器代码.
public static class MetaDataHelper
{
public static string GetDisplayName(Type dataType, string fieldName)
{
// First look into attributes on a type and it's parents
DisplayNameAttribute attr;
attr = (DisplayNameAttribute)dataType.GetProperty(fieldName).GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
// Look for [MetadataType] attribute in type hierarchy
// http://stackoverflow.com/questions/1910532/attribute-isdefined-doesnt-see-attributes-applied-with-metadatatype-class
if (attr == null)
{
MetadataTypeAttribute metadataType = (MetadataTypeAttribute)dataType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();
if (metadataType != null)
{
var property = metadataType.MetadataClassType.GetProperty(fieldName);
if (property != null)
{
attr = (DisplayNameAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
}
}
}
return …Run Code Online (Sandbox Code Playgroud) 我是MVC开发的新手,所以请耐心等待.是否真的有必要将我的局部视图命名为_Action.cshtml(使用_下划线)以符合命名约定?
这是我的问题我有一个控制器(StudentController)和一个动作(List),它有一个名为"List.cshtml"的部分视图文件,并且有
@{ Html.RenderAction("List", "Student"); }
Run Code Online (Sandbox Code Playgroud)
在我的HomeController - Index视图中显示这个部分视图.但如果我将我的局部视图命名_List.cshtml为当然它将无效.Visual Studio甚至找不到我的动作学生 - 列表的视图,因为它认为它仍然在寻找与我的动作完全相同的名称(List.cshtml).我该怎么办?
我习惯于使用ascx.cs配对的ASP.NET ascx.:(
asp.net-mvc-2 ×10
c# ×3
asp.net ×2
asp.net-mvc ×2
.net-3.5 ×1
badge ×1
firefox ×1
iis ×1
jquery ×1
jquery-post ×1
post ×1
razor ×1
validation ×1