Reputation: 10364
I am using Asp.Net MVC4. I have a html select in a view page. I want to maintain the selected value after postback.
View:
@using (Html.BeginForm("TaxMaster", "Masters", FormMethod.Get))
{
<div>
<select id="ddlSearchBy" name="ddlSearchBy" style="width: 150px">
<option value="TaxCode">Tax Code</option>
<option value="TaxDescription">Tax Description</option>
<option value="ClassDescription">Class Description</option>
<option value="ZoneName">Zone Name</option>
</select>
<input type="text" class="input-small" name="txtSearchValue" id="txtSearchValue" placeholder="Enter Search Value" style="width: 225px" /> 
<button type="button" id="btnSearch" class="btn btn-small btn-primary">Search</button>
</div>
}
MastersController.cs:
[HttpGet]
public ActionResult TaxMaster(string txtSearchValue, string ddlSearchBy)
{
TaxMaster objTaxTable = new TaxMaster();
objTaxTable.TaxTable = new List<moreInsights_offinvoice_taxmaster>();
objTaxTable.TaxTable = GetTaxMasterTable(ddlSearchBy, txtSearchValue);
return View(objTaxTable);
}
Here, In filter, I have one drop down, textbox and button. When I select the dropdown and click the search button, the selected value is passed to the controller class and it returns the filtered data to the view. But the drop down does not maintain the selected value. It again resets. How to maintain the drop down selected value in MVC?
Upvotes: 1
Views: 4872
Reputation: 11
<select class="form-control" id="TipoTarjeta" name="TipoTarjeta">
<option <%= Convert.ToString(Request.Form["TipoTarjeta"] != null ? Request.Form["TipoTarjeta"] : "" ) == "" ? "selected" : "" %>>Seleccione
</option>
<option <%= (Convert.ToString(Request.Form["TipoTarjeta"] != null ? Request.Form["TipoTarjeta"] : "").ToLower() == "visa" ? "selected" : "") %>>Visa</option>
<option <%= (Convert.ToString(Request.Form["TipoTarjeta"] != null ? Request.Form["TipoTarjeta"] : "").ToLower() == "mastercard" ? "selected" : "") %>>Mastercard</option>
<option <%= (Convert.ToString(Request.Form["TipoTarjeta"] != null ? Request.Form["TipoTarjeta"] : "").ToLower() == "american express" ? "selected" : "") %>>American Express</option>
<option <%= (Convert.ToString(Request.Form["TipoTarjeta"] != null ? Request.Form["TipoTarjeta"] : "").ToLower() == "otra" ? "selected" : "") %>>Otra</option>
</select>
Upvotes: 0
Reputation: 1901
public ActionResult TaxMaster(string txtSearchValue, string ddlSearchBy)
{
TaxMaster objTaxTable = new TaxMaster();
objTaxTable.TaxTable = new List<moreInsights_offinvoice_taxmaster>();
objTaxTable.TaxTable = GetTaxMasterTable(ddlSearchBy, txtSearchValue);
ViewBag.SelectedOption=ddlSearchBy;
return View(objTaxTable);
}
string selectedOption = ViewBag.SelectedOption;
<select id="ddlSearchBy" name="ddlSearchBy" style="width: 150px">
<option value="TaxCode" selected="@(selectedOption == "TaxCode" ? "selected" : "")">Tax Code</option>
<option value="TaxDescription" selected="@(selectedOption == "TaxDescription" ? "selected" : "")">Tax Description</option>
<option value="ClassDescription" selected="@(selectedOption == "ClassDescription" ? "selected" : "")">Class Description</option>
<option value="ZoneName" selected="@(selectedOption == "ZoneName" ? "selected" : "")">Zone Name</option>
</select>
Upvotes: 1