读书人

@Html.DropDownListFor 如何在客户端

发布时间: 2013-07-08 14:13:00 作者: rapoo

@Html.DropDownListFor 怎么在客户端验证
@Html.TextBoxFor(a => a.Title, new { style = "width:80%", maxlength = "80" })
可以实现在客户端验证

为什么 @Html.DropDownListFor( a => a.TypeId,(SelectList) ViewData["TypeId"],"—传达范围—")不行?


<tr>
<td width="15%">
标题:
</td>
<td colspan="3">@Html.TextBoxFor(a => a.Title, new { style = "width:80%", maxlength = "80" })
@Html.ValidationMessageFor(a => a.Title)
</td>
</tr>
<tr>
<td width="15%">
分类:
</td>
<td width="35%">
@Html.DropDownListFor( a => a.TypeId,(SelectList) ViewData["TypeId"],"—传达范围—")
@Html.ValidationMessageFor(a => a.TypeId)
</td>
</tr>




[MetadataType(typeof(TaskMD))]
public partial class Task
{


}
public class TaskMD
{
[Required(ErrorMessage = "必须选择类型")]
public int TypeId { get; set; }

[Required(ErrorMessage = "必须设置【任务名称】")]
public string Title { get; set; }
}

------解决方案--------------------


引用:
@Html.TextBoxFor(a => a.Title, new { style = "width:80%", maxlength = "80" })
可以实现在客户端验证

为什么 @Html.DropDownListFor( a => a.TypeId,(SelectList) ViewData["TypeId"],"—传达范围—")不行?

用nullable ( public int? TypeId),并且保证要在表单的里面。
保证 2个 jquery.validate.unobtrusive.js/jquery.validate.js 也引用了


public class TaskMD
{
[Required(ErrorMessage = "必须选择类型")]
public int? TypeId { get; set; }

[Required(ErrorMessage = "必须设置【任务名称】")]
public string Title { get; set; }
}

读书人网 >asp.net

热点推荐