回发后如何维护下拉列表选择的值?
How to maintain dropdownlist selected value after postback?
我正在使用 Asp.Net MVC4。我在视图页面中有一个 html select。我想在回发后保持 selected 值。
查看:
@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);
}
在这里,在过滤器中,我有一个下拉列表、文本框和按钮。当我 select 下拉菜单并单击搜索按钮时,selected 值被传递给控制器 class 并且它 returns 过滤后的数据被传递给视图。但是下拉列表不会保持 selected 值。它再次重置。如何在 MVC 中维护下拉 selected 值?
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>
<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>
我正在使用 Asp.Net MVC4。我在视图页面中有一个 html select。我想在回发后保持 selected 值。
查看:
@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);
}
在这里,在过滤器中,我有一个下拉列表、文本框和按钮。当我 select 下拉菜单并单击搜索按钮时,selected 值被传递给控制器 class 并且它 returns 过滤后的数据被传递给视图。但是下拉列表不会保持 selected 值。它再次重置。如何在 MVC 中维护下拉 selected 值?
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>
<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>