[英]Form does not perform GET on submit
My model contains: 我的模型包含:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
My controller contains: 我的控制器包含:
public ActionResult Index(int? personId)
{
var people = db.People; // all people in my database
ViewBag.People = new SelectList(people, "Id", "Name", personId);
return View();
}
My view contains: 我的观点包含:
@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" })
}
And I have this piece of Javascript: 我有一段JavaScript:
<script type="text/javascript">
(function ($) {
$(".select-submit").change(function () {
$(this).parents("form").submit();
alert("form submitted!");
});
})(jQuery);
</script>
When I select a person from the DropDownList
, I see the alert
popup, and the page refreshes. 当我从DropDownList
选择一个人时,我看到alert
弹出窗口,并且页面刷新。 When I select the default option ( "All People"
), I also see the popup, but the page does not refresh. 当选择默认选项( "All People"
)时,我还会看到弹出窗口,但页面不会刷新。
I want "personId"
to be set to ""
on selecting the default option. 我希望在选择默认选项时将"personId"
设置为""
。 How can I achieve this? 我该如何实现?
Ah, I found it. 啊,我找到了。 After inspecting the html, I noticed the form was using data validation. 在检查了html之后,我注意到该表单正在使用数据验证。 These html attributes were getting added after selecting a person: 选择人员后,将添加以下html属性:
data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required."
The form couldn't be submitted because ""
is not valid for an Int32
. 无法提交表单,因为""
对于Int32
无效。 Adding this html attribute to the DropDownList
solved it: 将此html属性添加到DropDownList
解决该问题:
data_val = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.