2015-11-04 67 views
1

我正在使用Asp.Net MVC4。我在查看頁面中有一個html選擇。我想在回發後保持選定的值。如何在回發後維護dropdownlist選定值?

查看:

@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" />&ensp; 
    <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

它可以幫助你:http://stackoverflow.com/questions/6981698/how-to-keep-dropdownlist-selected-value-after-postback –

+0

只是回答[這個類似的問題](http://stackoverflow.com/questions/33488883/asp-net-mvc-filter-view-with-selected-combo/33510236#33510236) –

回答

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> 
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>

相關問題