Reputation: 303
I have a ViewModel that I'm trying to pass to my controller which contains a dictionary. I'm able to get everything to show up correctly in the view, however when I got to post to my controller I never get the the StudentKeys dictionary to go through, however all other information goes through fine.
View
@using (Html.BeginForm("BulkEditStudentRecordsAdultEd", "Order", FormMethod.Post, new { @id = "EditStudentAdultEd", @class = "form-horizontal" }))
{
@Html.HiddenFor(m => m.AdultEdAgencyCd)
foreach (var item in Model.StudentKeys)
{
@Html.HiddenFor(m => item.Key)
@Html.HiddenFor(m => item.Value)
}
<h1>@Model.StudentKeys.Count</h1>
<div class="row inputRow">
@Html.BootstrapDropDownListFor(m => m.DistrictCd, Model.DistrictCodes, "col-md-5", labelText: "District:", htmlAttributes: new { @id = "DistrictCd" })
@Html.BootstrapDropDownListFor(m => m.SchoolCd, Model.SchoolCodes, "col-md-5", labelText: "School:", htmlAttributes: new { @id = "SchoolCd" })
</div>
<div class="col-md-offset-2">
<button id="SaveStudentBulkEdit" type="submit" class="btn btn-primary btn-sm">Save <span class="badge">0</span></button>
<button id="CancelStudentBulkEdit" type="button" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
</div>
}
Controller
[HttpPost]
[CustomAuthorize(DiplomaRoles.SchoolStaff, DiplomaRoles.StateAdultEdManagement, DiplomaRoles.SchoolAdmin, DiplomaRoles.StateAdmin)]
public ActionResult BulkEditStudentRecordsAdultEd(EditStudentAdultEdViewModel vm) //All other information from my view model gets through
{
var keys = vm.StudentKeys; //Empty
var DistrictCd = vm.DistrictCd; //Ok
var SchoolCd = vm.SchoolCd; //Ok
}
View Model
public class EditStudentAdultEdViewModel
{
public EditStudentAdultEdViewModel()
{
StudentKeys = new Dictionary<string, string>();
}
public string DistrictCd {get; set;}
public string SchoolCd {get; set;}
public string IdentificationNbr{ get; set; }
public int DuplicateNbr { get; set; }
public Dictionary<string, string> StudentKeys { get; set; }
public string AdultEdAgencyCd { get; set; }
}
Upvotes: 2
Views: 1019
Reputation: 2982
MVC already puts everything that was submitted into a dictionary for you.
public ActionResult BulkEditStudentRecordsAdultEd(FormCollection vm)
{
// to access an item, do this: vm["inputName"]
}
If you want the default modelbinder to bind for you into a different dictionary, you can do so by having inputs with correct names:
<input type="hidden" name="vm[KeyString]" value="ValueString" />
Upvotes: 2