//Model
public class SelectModel
{
public string CategoryId { get; set; }
public List<SelectListItem> List { get; set; }
}
//ViewModel
public class ViewModel
{
public SelectModel SelectMod { get; set; }
}
//OnIndex call
@Html.Partial("ViewUserControl1", Model.SelectMod)
//ViewUserControl1.cshtml
@model MvcApplication4.Models.SelectModel
<div id="formid">
@using (Ajax.BeginForm("Index1", "Home", new AjaxOptions { UpdateTargetId = "formid" }, new { id = "TheForm" }))
{
@Html.DropDownListFor(x => x.CategoryId, Model.List, "--Select One--")
<input type="submit" name="name" value="Submit" />
}
</div>
//Controller
public ActionResult Index1(SelectModel sm)
{
return PartialView("ViewUserControl1", sm);
}
So now when you post the Ajax.BeginForm we get the selectedId of the form and not the FULL DROPDOWN LIST.
If i have many dropdown list do i need to create the sm again with full dropdown list.
Is there a way where i can send the whole dropdown list to the controller and return back the same.
Update :
Should’t there be a param in AjaxOptions which allows us to post the full list(if we need) of the page along with the selectedIds.
The short answer is no! Since dropdownlists in HTML don’t return all their members, just the selected one(s).
The longer answer is that ASP.NET WebForms allows you to do what you ask for, and you could recreate some of that by creating a similar mechanism as the ViewState is in ASP.NET WebForms.
What ASP.NET WebForms does is that it stores all the values from the list in the page twice. Both in the list for display, and also a copy in a hidden field called the ViewState. The hidden field is then sent back to the server on submit and used to repopulate the dropdownlist.
You could mimick the same behaviour ASP.NET WebForms uses in MVC by serializing your collection in some way and putting it in a hidden field. Then on submit, you can deserialize the data from the hidden field back into the collection and use it to populate the dropdown.