I have a MVC4 page that has a form with a collection of checkboxes, radio buttons and textboxes used as the search fields. Upon post the selections are parsed and the lower results grid is updated with new results. Right now all the form values are wiped out upon return and the new results are displayed in the grid - only the grid is part of the model.
I want all the form selections to retain their values after post so the user can see (and change) the selections for next post/search. The form is popuplated with viewbags.
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "searchform" }))
{
@Html.ValidationSummary("Please correct the following errors")
<div style="float:left;">
<div style="float:left;">
<label>Name:</label>
@Html.TextBox("name")
</div>
<div style="float:left; margin-left:15px">
<label>Company:</label>
@Html.TextBox("company")
</div>
<div style="float:left; margin-left:65px">
<label>Date Range:</label>
@Html.TextBox("dateStart", "", new { @class = "datefield", type = "date" })
to
@Html.TextBox("dateEnd", "", new { @class = "datefield", type = "date" })
</div>
</div>
<div style="clear: both;">
Match Any Categories? <input type="radio" name="categoryMatchAll" value="false" checked="checked" />
Match All Categories? <input type="radio" name="categoryMatchAll" value="true" />
</div>
<div style="float:left;">
<div id="searchform-categories" style="float:left;">
<div class="scroll_checkboxes">
<label>Categories</label>
<ul>
@foreach (var x in ViewBag.Categories)
{
<li>
<input type="checkbox" name="categories" value="@x.Id"/>
@x.Name
</li>
}
</ul>
</div>
</div>
<div id="searchform-diversity" style="float:left; margin-left:30px">
<div class="search-selection" style="float:left;">
<label>Minority Owned</label>
<ul>
@foreach (var x in ViewBag.Minorities)
{
<li>
@Html.RadioButton("minorities", (String)x.Id.ToString())
@x.Name
</li>
}
</ul>
</div>
<div class="search-selection" style="float:left;">
<label>Diversity Class</label>
<ul>
@foreach (var x in ViewBag.Classifications)
{
<li>
@Html.RadioButton("classifications", (String)x.Id.ToString())
@x.Name
</li>
}
</ul>
</div>
</div>
</div>
<div style="clear: both;">
<input type="submit" value="Search Profiles" />
<input type="submit" value="Reset" />
</div>
}
the data grid is bound to the model as
@model IEnumerable<VendorProfileIntranet.Models.VendorProfile>
<table id="VendorTable" width="100%" class="gradeA">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.CompanyName)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.State)
</th>
<th>
@Html.DisplayNameFor(model => model.DateCreated)
</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td class="list-field">
@Html.DisplayFor(modelItem => item.Name)
</td>
<td class="list-field">
@Html.DisplayFor(modelItem => item.CompanyName)
</td>
<td class="list-field">
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.State)
</td>
<td class="list-field">
@Html.DisplayFor(modelItem => item.DateCreated)
</td>
<td class="list-field">
@Html.ActionLink("Edit", "Edit", new { id = item.ProfileID }) |
@Html.ActionLink("View", "View", new { id = item.ProfileID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ProfileID }, new { onclick = " return DeleteConfirm()" })
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td> </td>
</tr>
</tfoot>
PHP - Keep The Values in The Form To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the <textarea> and </textarea> tags.
If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState. Clear() to clean the ModelState values. The reason is that normally, you want to postback to the client the form with all the errors.
if you are using html in mvc then check solution 2 from here, value="@Request["txtNumber1"]"
worked fine for me,
<input type="text" id="txtNumber1" name="txtNumber1" value="@Request["txtNumber1"]"/>
hope helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With