In my view model, I have a list of objects. I repeat these objects and create controls for each of them. In the situation below, I want to show people a text box and a button for each object. When the user clicks the button, a message is created and I can save the data in the controller.
In the user interface, the user can change the desired form and click "Save."
My problem is that the model is null when it is sent to the controller.
My Razor Code :
using (Html.BeginForm()) { foreach (var contributor in Model.Contributor) { @Html.HiddenFor(model => contributor.Id) <div class="formrow"> @Html.ValidationSummary(true) </div> <h2>@Html.TextRaw("AuthorInfo", "Author")</h2> <div class="formrow"> @Html.EditorFor(model => contributor.FirstName) <div class="formvalidation"> @Html.ValidationMessageFor(model => contributor.FirstName) </div> </div> <div class="formrow right"> <input type="hidden" name="formsubmitted" value="true" /> <input type="submit" class="button" value="@Html.Text("ButtonText", "Save")" /> </div> } }
The code for my view model
public class ProfileModel { public string Message { get; set; } public List<PublisherModel> Publisher { get; set; } public List<ContributorModel> Contributor { get; set; } public ContributorModel NewContributor { get; set; } }
My controller code
[HttpPost] public ActionResult Mine(ProfileModel model, string newuser) {
How to fix it?
I assume that I need to expand the model of my view in order to somehow save the changes. But I really don't understand how to do this.
Currently, all properties in ProfileModel are null when it reaches the controller.
Any ideas?
source share