Does the name of parameter have to be model?

2019-01-09 19:01发布

问题:

Hit a strange issue where my model is not binding and shows up on the controller as null.

I have a form doing a httppost. My breakpoint in the controller is hit and the parameter I expect to be my model is null.

Looking at some example code on another page that works, I copied and pasted it and the only difference was the name of the parameter was 'model' instead of message.

View

@model Site.Models.ContactMessage

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ContactMessage</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.To, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.To, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.To, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Controller

    public ActionResult Contact()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Contact(ContactMessage message)
    {
        var m = message;
        return View();
    }

and it worked. I thought I must have entirely missed something about naming convention. Found you can use Bind, from reading a heap of other posts, to change the prefix like;

    public ActionResult Contact([Bind(Prefix = "model")] ContactMessage message)

but that didn't work, still null. Going to rename it to model so it works and I can move on but would like to know why it's not binding if not called model.

    public ActionResult Contact(ContactMessage message)

Changed back to this as above but still returns a null. Interestingly, if I open up another MVC app, that one has whatever parameter names I want and works fine. It's using an older version of MVC 5 (not updated it yet but I will do that and see if anything happens. I don't expect it will.)

回答1:

Your problem is that you model contains a property named Message and you also have a parameter named message The DefaultModelBinder reads the form values which will include message = "someTextValue" and searches for model properties that have the name message. It finds the one in you model and sets it value (all OK so far) but then it finds another one (your parameter) and tries to set the value of a complex object string value (in effect ContactMessage message = "someTextValue";) which fails so the model becomes null