loki
loki

Reputation: 2966

How to keep selected value after clicking submitbutton in asp.net mvc 3?

Hi; i have 2 dropdownlist. one is customers ddl another one is jobs. i want to keep alive selected value after any selection process. But i can not. i want to keep selected value on ddl'selected item After click button. (i have been used 2 methods: dropdownlist and dropdownlistfor.) My favorite articles are http://stackoverflow.com/questions/6981698/how-to-keep-dropdownlist-selected-value-after-postback and http://stackoverflow.com/questions/4950798/how-to-post-the-selected-value-of-a-selectlist-to-the-controller-using-a-view-mo

ViewModel:


 public class MyViewModel
    {
        public IEnumerable<string> Columns { get; set; }
        public IEnumerable<dynamic> Values { get; set; }
        public bool HasNext { get; set; }
        public bool HasPrevious { get; set; }
        public IEnumerable<CustomerModel> Customers { get; set; }
        public IEnumerable<SelectListItem> Jobs { get; set; }
    }
  

Cotroller:


      public class JobController : Controller
      {
        public ActionResult Index(int? page)
        {
            var model = new MyViewModel();
            model.Customers = CustomerOperation.GetCustomers().Items;
            ViewData["Jobs"] = new SelectList(JobOperation.GetCustomersAssemblyList().Items, "scheduleId", "name", null);
            return View(model);
        }

View:



 <table style="padding:25px; margin:10px 10px 10px 10px; width:800px" id="sample">
                <tr>
                <td>Customer Name: </td>
                <td>
               <%= Html.DropDownListFor(X=>X.Customers,new SelectList(Model.Customers,"Id","Name"),"** Please Select **", new { id = "ddlCustomers" })%>
               </td>
                <td>Job Name:</td>
                <td>
              <%= Html.DropDownList("Jobs", (IEnumerable<SelectListItem>)ViewData["Jobs"], "** Please Select **", new { id = "ddlJobs", disabled = "disabled" })%>
                </td>
                <td>
                 <input value="monitor" name="submitButton" type="submit" />
                </td>
                </tr>
                </table>

Upvotes: 0

Views: 2092

Answers (1)

SBB
SBB

Reputation: 155

Your model should be something like this

public class MyViewModel
{
    public IEnumerable<string> Columns { get; set; }
    public IEnumerable<dynamic> Values { get; set; }
    public bool HasNext { get; set; }
    public bool HasPrevious { get; set; }
    public IEnumerable<CustomerModel> Customers { get; set; }
    public IEnumerable<SelectListItem> Jobs { get; set; }

    public CustomerModel Customer {get; set;}
}

In your view the code to add dropdown should be something like this

<%= Html.DropDownListFor(X=>X.Customer,Model.Customers) %>

Hope this helps.

Upvotes: 1

Related Questions