【发布时间】:2014-12-04 00:09:17
【问题描述】:
除非我的 TextBoxFor 字段中有值,否则我的 MVC 表单将不会提交。我使用 TextBoxFor 是因为我确实希望将字段的值存储在模型属性中。我需要以下两种解决方案之一,任何一种都可以满足我的需求。我需要在提交表单时将常规 TextBox 的值保存到模型属性,或者在我的 TextBoxFor 为空时允许提交。
目前我的代码如下所示:
@using (Ajax.BeginForm("PartialResults", "Students", new { LoadItemsOnly = true }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "find-results" }))
{
<div class="inline-ct">
<table>
<tr>
<td><span class="lbl">Name: </span></td>
@Html.TextBoxFor(m => m.Filter, new { autofocus = "autofocus", id = "name" })
</tr>
<tr>
<td><span class="lbl">@Html.DisplayNameFor(m => m.LocationID): </span></td>
<td>@Html.DropDownListFor(m => m.LocationID, new SelectList(Model.Locations, "Key", "Value"))</td>
</tr>
<tr>
<td><span class="lbl">@Html.DisplayNameFor(m => m.ADGuid): </span></td>
<td>@Html.DropDownListFor(m => m.ADGuid, new SelectList(Model.Teachers, "Key", "Value"))</td>
</tr>
<tr>
<td><span class="lbl">@Html.DisplayNameFor(m => m.ClassCode): </span></td>
<td>@Html.DropDownListFor(m => m.ClassCode, new SelectList(Model.SchoolClasses, "Key", "Value"))</td>
</tr>
<tr>
<td><button type="submit" class="btn-primary" id="btn-find">Find</button></td>
<td></td>
</tr>
</table>
</div>
<div id="student-find-results" class="row">
</div>
}
如前所述,当有 TextBoxFor 值时,这可以正常工作,但是如果为空,则不会提交表单。任何帮助将不胜感激。
注意: 我尝试在按钮上添加一个 onclick,但它没有提交给控制器。
【问题讨论】:
-
请查看您的模型,看看它是否标有
[Required]数据注释。以及为什么要更改Filter的id -
哇...我的脸有点红。这解决了它,谢谢。如果你把它作为答案,我会这样标记。感谢您的帮助!
标签: c# asp.net-mvc-4 razor html.textboxfor