`Model.InValid'。错误来自下拉列表。
[Required]
[Display(Name = "Country")]
public List<Countries> Countries { get; set; }
public string CountryCode { get; set; }
<div class="form-group">
<div class="col-sm-5">
@Html.LabelFor(model => model.Countries, new { @class = "control-label" })
<span class="red">*</span>
</div>
<div class="col-sm-7">
@Html.DropDownList("CountryCode", new SelectList(Model.Countries,
"CountryCode", "CountryDesc"), new { @class = "btn btn-default
dropdown-toggle" })
@Html.ValidationMessageFor(model => model.Countries, "",
new { @style = "color:Red" })
</div>
</div>
[HttpPost]
public JsonResult Index(Contact contact)
{
if (ModelState.IsValid) // Alway is false, because countries prop is null
{
//code here
var result = new { Success = "True", Message = "No Error" };
return Json(result, JsonRequestBehavior.DenyGet);
}
else
{
var result = new { Success = "False", Message = "Invalid state" };
return Json(result, JsonRequestBehavior.DenyGet);
}
}
怎么过来这个?帖子list<courties> Countries
中的内容为空。
答案 0 :(得分:3)
您在列表中需要而不是所选值。应该是
[Display(Name = "Country")]
public List<Countries> Countries { get; set; }
[Required]
public string CountryCode { get; set; }