My form is not returning a null model.
I am viewing the details of a specific model on a Details page and then, if a user presses the SubmitPost button, the view should return the model.
View
@model MyApp.Models.MyModel
@using (Html.BeginForm("ApplyUsingDetails", "Controller", FormMethod.Post))
{
<fieldset>
<legend>Course</legend>
<div class="display-label">MyModel ID</div>
<div class="display-field">
@Html.DisplayFor(model => model.CourseID)
</div>
<div class="display-label">Title</div>
<div class="display-field">
@Html.DisplayFor(model => model.Title)
</div>
etc. . .
<p><input type="submit" value="SubmitPost" /></p>
<more misc information for the view>
</fieldset>
}
Action
[HttpPost]
public ActionResult ApplyUsingDetails(MyModel model)
{
// when I try to access model here it comes up null
}
To give some context: I am using this form to allow a student to view the details of a class and then apply to the class at the click of a button.
Any clue as to why my model would be null?
Based on the sample you provided, it doesn’t look like you are including any of
MyModel‘s fields in the form. As a test, try including a hidden field or use@Html.HiddenFor(model => model.CourseID)and see if you have any values posted.