Ecnalyr
Ecnalyr

Reputation: 5802

I am attempting to return a model from a form that shows the details of said model but am returning a null value

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?

Upvotes: 1

Views: 191

Answers (3)

Brian Cauthon
Brian Cauthon

Reputation: 5534

You need to include input elements in your form so that they can be posted to your controller and bound to your controller's model. Model Binding in ASP.Net MVC takes the form values that you submitted and looks for a property on your controller's model with the same name and tries to set the propery with the form value. No input fields in your form means nothing for Model Binding to stick into the controller's model.

I would suggest using readonly input elements styled with css to look like div.display-field.

    <div class="display-label">Course ID</div> 
    <div> 
        @Html.TextBoxFor(model=>model.CourseID, new{readonly="readonly", @class="display-field"}) 
    </div> 

Wrapping the input in a div may or may not be needed depending on other styles on the page.

Upvotes: 0

Shyju
Shyju

Reputation: 218722

DisplayFor HTML helper will simply return a string that represents the Property value usually. If your Property is a boolean type, it wll render it as a disabled checkbox. As long as you dont have any input controls you wont be getting the value. So in this case, you can simply use the @Html.HiddenFor HTML helper method.

HTML.HiddenFor Helper Method generates HTM Markup for an input element of type hidden with the id and name same as the Expression

@using (Html.BeginForm("ApplyUsingDetails", "Controller", FormMethod.Post))
{
   <div class="display-label">MyModel ID</div>
   <div class="display-field">
        @Html.DisplayFor(model => model.CourseID)
        @Html.HiddenFor(m=>m.CourseId)
   </div>
   <input type="submit" value="Save" />
}

Now the Value of Course id will be available in your HTTPPost action method.

Upvotes: 0

Jonathan Bates
Jonathan Bates

Reputation: 1835

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.

Upvotes: 1

Related Questions