Pass Values from View to Controller MVC4.0 Multi Select Dropdownc#
I am new to MVC and I have view for search record as below
@using (Html.BeginForm("SearchRecord", "Home", FormMethod.Post))
{ <table class="table table-striped">
<tbody>
<tr>
<td>
@Html.TextBox("txtCallerId", Model.callerNo, new { placeholder = "Caller Number", @class = "form-control" })
</td>
</tr>
<tr>
<td>
<div id="divlstCampaigns">
<select id="lstCampaigns" name="lstCampaigns[]" multiple="multiple">
<select id="lstCampaigns" name="lstCampaigns[]" multiple="multiple" class="form-control">
@foreach (var item in Model.cmpList)
{
<option>@item</option>
}
</select>
</select>
</div>
</td>
</tr>
<tr>
<td><button type="submit" class="btn btn-default bg-info" value="Submit">Search</button> </td>
</tr>
</tbody>
</table>
}`
My Model is :
public class lstSearchCriteria
{
public List<string> cmpList { get; set; }
public string callerNo { get; set; }
// ......
}
Controller
public ActionResult SearchRecord()
{
string userName = TempData["user"] as string;
instance = TempData["instance"] as IInstance;
......
lstSearchCriteria objSearc = new lstSearchCriteria();
objSearc.agentList = agentList;
objSearc.cmpList = campaignList;
return View(objSearc);
}
[System.Web.Mvc.HttpPost]
public ActionResult SearchRecord(lstSearchCriteria form)
{
List<lstCampaign> lstCmp = form.cmpList;//NULL Value here
string callerNo = form.callerNo;//Value Present
return View();
}`
What I expect is when I click on the submit or Search button. Values from text box & drop-down boxes should be visible/passed to the controller. I am getting value for text box but for drop down values I am getting null array. I am using bootstrap-multiselect drop down.
Posted as-is, you're generating invalid markup (a <select> inside of a <select> is very much invalid - the only valid children of <select> are <optgroup> and <option>), nor do your field names match the model you say you're using. The modelbinder in the MVC Framework matches form values to model properties by the name attribute.
This is probably a better fit as a Stack Overflow question, and honestly doesn't have anything to do with the multiselect plugin, but here's some suggestions:
- Rewrite your model like so:
public class lstSearchCriteria
{
public lstSearchCriteria()
{
cmpList = new MultiSelectList(Enumerable.Empty<string>());
lstCampaigns = Array.Empty<string>();
}
public MultiSelectList cmpList { get; set; }
public string[] lstCampaigns { get; set; }
public string callerNo { get; set; }
}
cmpList will be your list of available options. lstCampaigns will hold the selected values in the posted form. I've initialized both in the constructor of the model, so that view generation will always work. You'll need to update your GET action to populate the values of cmpList (and lstCampaigns, if you want some values preselected). MultiSelectList has a few different constructors, with the simplest just requiring a collection of options (if you pass a collection of strings, the values of the string collection will be used as both the text and the value of the generated options).
- Your form will need to be fixed. Assuming you update the model as noted in (1), then:
@using (Html.BeginForm("SearchRecord", "Home", FormMethod.Post))
{
<table class="table table-striped">
<tbody>
<tr>
<td>
@Html.TextBoxFor(m => m.callerNo, new { placeholder = "Caller Number", @class = "form-control" })
</td>
</tr>
<tr>
<td>
<div id="divlstCampaigns">
@Html.ListBoxFor(m => m.lstCampaigns, Model.cmpList, new { @class = "form-control" })
</div>
</td>
</tr>
<tr>
<td>
<button type="submit" class="btn btn-info">Search</button>
</td>
</tr>
</tbody>
</table>
}
I prefer, when possible, to use the strongly-typed helpers, since then we know that the generated markup will bind correctly when submitting the form.
HtmlHelper.ListBox (and ListBoxFor) generate <select multiple> markup and handle the multiple <option> selection, so that's taken care of for you.
Once you make these changes, you should see your posted values in the controller action.
Thanks this works great. Actually select inside select was typing / copy paste mistake.