简体   繁体   中英

How to maintain dropdownlist selected value after postback?

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" />&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);
  }

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?

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM