我有一个使用 MVC4 的基于自定义属性的验证
我可以使用 propertyinfo[] 使用以下代码在文本框中获取用户输入的值
PropertyInfo textBoxEnteredValue = validationContext.ObjectType.GetProperty("TxtCrossField");
但我无法获得用户选择的下拉值。
是否需要进行任何代码更改,请建议
将
object value
作为 NULL 进入该IsValid
方法。知道为什么会这样吗?
验证
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//Working
PropertyInfo textBoxEnteredValue = validationContext.ObjectType.GetProperty("TxtCrossField");
//How to get the selected item?
PropertyInfo selectedDropdownlistvalue = validationContext.ObjectType.GetProperty("DDlList1");
}
模型
public class CrossFieldValidation
{
public string DDlList1
{ get; set; }
// [Required(ErrorMessage = "Quantity is required")]
[ValueMustbeInRange]
[Display(Name = "Quantity:")]
public string TxtCrossField
{ get; set; }
}
看法
@model MvcSampleApplication.Models.CrossFieldValidation
@{
ViewBag.Title = "DdlCrossFields";
}
<h2>DdlCrossFields</h2>
@using (Html.BeginForm("PostValues", "CrossFieldsTxtboxes"))
{
@Html.ValidationSummary(true)
<div class ="editor-field">
@Html.TextBoxFor(m => m.TxtCrossField)
@Html.ValidationMessageFor(m=>m.TxtCrossField)
</div>
@*@Html.DropDownList("DDlList1",(IEnumerable<SelectListItem>)ViewBag.itemsforDropdown)*@
@Html.DropDownList("ItemsforDrop", ViewBag.ItemsforDrop as SelectList,"Select A state", new {id= "State"})
<input id="PostValues" type="Submit" value="PostValues" />
}
有没有人请提出任何关于这个的想法...非常感谢....