DeepakJ
DeepakJ

Reputation: 388

Form post not working on submit. Form object shows null

When I post the form by clicking on the save button, it hits the post method but the model parameter is always null.

Controller:

[HttpPost]
public ActionResult Edit(QuestionMaster question)
{  
    if (questionLogic.Update(model))
    {
        return RedirectToAction("List");
    } 
    return View();
}

View:

@using (Html.BeginForm()) {
    <fieldset>
        <legend>QuestionMaster</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Question)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Question)
            @Html.ValidationMessageFor(model => model.Question)
        </div>
    </fieldset>
    <p><input type="submit" value="Save" /></p>
}

Upvotes: 0

Views: 2768

Answers (4)

user3559349
user3559349

Reputation:

You have not posted your model for QuestionMaster but from the view code it appears to contain a property named Question which is typeof string. The problem is that your POST method parameter is also named question which causes model binding to fail and the object is null.

Rename the parameter to anything but a name of a property in your model, for example

[HttpPost]
public ActionResult Edit(QuestionMaster model)

The reason why your model was null on postback is because of how model binding works

  1. The DefaultModelBinder initializes a new instance of QuestionMaster
  2. The posted form's name/value pairs are then checked. If a matching property name is found, the value of that property is set.
  3. In your case your posting back Question="The text you entered" The model binder finds the parameter named question (i.e. a match) and sets it to "The text you entered", but question is typeof QuestionMaster (a complex object, not a string) so binding fails and the model becomes null.

Upvotes: 3

Lucas L Roselli
Lucas L Roselli

Reputation: 2830

You can do it what vortex told you or just change your parameter name to anything but "question"

[HttpPost]
public ActionResult Edit(QuestionMaster question2)

or

[HttpPost]
public ActionResult Edit(QuestionMaster model)

or

[HttpPost]
public ActionResult Edit(QuestionMaster anything)

Upvotes: 0

vortex
vortex

Reputation: 1058

Pass HtmlFieldPrefix in your EditorFor.

@Html.EditorFor(model => model.Question, new ViewDataDictionary() { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "QuestionMaster" }})

This way the names of the fields will be correct and the model binder will be able to bind them.

Upvotes: 1

Inspector Squirrel
Inspector Squirrel

Reputation: 2548

From the code you've given above, your View is missing a model.

You can add the model to the view as below:

@model QuestionMaster

This code is typically the first line in your view.

Other than that, can you explain the scope of model in your controller action? Where is model defined? If it isn't defined, you should understand that using @Model.someValue in the view is fine, but accessing model in your controller won't work unless your posted model parameter is called model.

Assuming that may be another reason for your form being "null", try changing your controller to:

[HttpPost]
public ActionResult Edit(QuestionMaster question)
{  
    if (questionLogic.Update(question))
    {
        return RedirectToAction("List");
    } 
    return View();
}

Upvotes: 0

Related Questions