我使用的是Asp.Net MVC4。我在视图页面中有一个html选择。我想在回发后保持选定的值。
查看:
@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);
}
在这里,在过滤器中,我有一个下拉菜单,文本框和按钮。当我选择下拉列表并单击搜索按钮时,所选值将传递给控制器类,并将筛选后的数据返回到视图。但下拉列表不会保持所选值。它再次重置。如何在MVC中维护下拉选择值?
答案 0 :(得分:1)
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>
答案 1 :(得分:0)
<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>