I am using ViewModel to retrieve the input data into a controller action. But the ViewModel gets empty values ββin the properties. I create one partial view
and in this partial view I create drop-down lists binding the ViewModel and then I rendering that partial view in another View >
Below is my code
My ViewModel:
public class LookUpViewModel { RosterManagementEntities rosterManagementContext = new RosterManagementEntities(); public LookUpViewModel() { tblCurrentLocations = from o in rosterManagementContext.tblCurrentLocations select o; tblStreams = from o in rosterManagementContext.tblStreams select o; } [Required] public virtual IEnumerable<tblCurrentLocation> tblCurrentLocations { get; set; } [Required] public virtual IEnumerable<tblStream> tblStreams { get; set; }
My partial view:
@model PITCRoster.ViewModel.LookUpViewModel @Html.Label("Location") @Html.DropDownListFor(M=>M.tblCurrentLocations, new SelectList(Model.tblCurrentLocations, "LocationId", "Location"), "Select Location") @Html.ValidationMessageFor(M=>M.tblCurrentLocations) <br /> @Html.Label("Stream") @Html.DropDownListFor(M => M.tblStreams, new SelectList(Model.tblStreams, "StreamId", "Stream"), "Select Streams") @Html.ValidationMessageFor(M => M.tblStreams)
My view in which I do this partial view
@{ ViewBag.Title = "Resources"; } <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> <h2>Resources</h2> @using (Html.BeginForm("AddResource", "Resources", FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) Html.RenderPartial("_LookUpDropDowns", new PITCRoster.ViewModel.LookUpViewModel()); <br /> <input type="submit" value="Create" /> }
And this is my controller action method: [HttpPost]
public void AddResource(LookUpViewModel testName) {
When I put the debugger in my controller action control method, it goes to this Action method. But the ViewModel object has null in it. I tried to access the entered values ββusing FormCollection and I get all the data as expected ...
Below is my code for controller action with FormCollection
[HttpPost] public void AddResource(FormCollection form) { var x = form["tblStreams"];
Can someone explain to me why I am not getting the value in the ViewModel? Thanks...