我的模型包含:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
我的控制器包含:
public ActionResult Index(int? personId)
{
var people = db.People; // all people in my database
ViewBag.People = new SelectList(people, "Id", "Name", personId);
return View();
}
我的观点包含:
@using (Html.BeginForm(null, null, FormMethod.Get, new { @class = "form-inline" }))
{
@Html.DropDownList("personId", ViewBag.People as SelectList, "All people", new { @class = "form-control select-submit" })
}
我有这条Javascript:
<script type="text/javascript">
(function ($) {
$(".select-submit").change(function () {
$(this).parents("form").submit();
alert("form submitted!");
});
})(jQuery);
</script>
当我从DropDownList
中选择一个人时,我会看到alert
弹出窗口,页面会刷新。当我选择默认选项("All People"
)时,我也会看到弹出窗口,但页面没有刷新。
我希望在选择默认选项时将"personId"
设置为""
。我怎样才能做到这一点?
答案 0 :(得分:1)
data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required."
由于""
对Int32
无效,因此无法提交表单。将此html属性添加到DropDownList
解决了它:
data_val = false