I have include some models in my asp.net mvc4 view so I have create a base view model which contains the two other models:
namespace MyNamespace.Models
{
public class CustomViewModel
{
public FirstTypeModel FirstViewModel { get; set; }
public SecondTypeModel SecondViewModel { get; set; }
}
}
and the view:
@model MyNamespace.Models.CustomViewModel
@using (Html.BeginForm("AddFields", "Configure", FormMethod.Post))
{
(...)
<div id="componentId">
@Html.LabelFor(m => m.FirstViewModel.SelectedCompTypeId, new { @id = "componentIdLabel" })
@Html.DropDownListFor(m => m.FirstViewModel.SelectedCompTypeId, Model.FirstViewModel.CompTypeItems, new { @name = "SelectedCompTypeId", @id = "componentType" })
</div>
(...)
<input id="submitAddComp" type="submit" value="@Resource.ButtonTitleAddComponent" />
}
in my controller:
public ActionResult AddFields(string param1, string param2, string param3, int selectedCompTypeId)
{
...
}
when click on the submit button I am getting selectedCompTypeId as null (param1, param2 and param3 are passed correctly), but if I watch below request from within the controller it has the correct value:
Request["FirstViewModel.SelectedCompTypeId"]
so how to pass the correct parameter to the controller in order to selectedCompTypeId not to be null?
Note: including only one model, before creating the base model which contains the others two, it was working correctly. Before, lamba expression was m => m.SelectedCompTypeId instead m => m.FirstViewModel.SelectedCompTypeId.