My MVC form will not submit unless I have a value in my TextBoxFor field. I am using the TextBoxFor because I do want the value of the field stored in a model attribute. I need one of the two following solutions, either one satifies my needs. I either need, on submission of my form to be able to save the value of a regular TextBox to the model attribute or allow to submit when my TextBoxFor is empty.
Currently my code looks like this:
@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>
}
As mentioned this works fine when there is a value the TextBoxFor, however the form is not submitted if empty. Any help would be greatly appreciated.
NOTE: I tried adding an onclick even to the button but then it doesn't submit to controller.
First of all check if you're not adding a [required] attribute on the field in your ViewModel Object that you pass to the view:
Second, make sure that your text-box field is nullable using the ? keyword in your ViewModel like that
Third, Check if there is JQuery validation over the fields on client side. Note that if you want to disable validation for all your fields, you can modify your controller to not test for ModelState by removing the ModelState check: