简体   繁体   中英

Why is my model not passed as parameter with form post

I'm having trouble understanding why my model is not passed along with its values to my controller when posting a form.

I have a view with a strongly typed model (UnitContract) that is being fetched from a webservice, that holds a set of values. In my action I'm trying to fetch int ID and bool Disabled fields that exists in my model. When debugging, I see that my model being passed from the form doesn't contain any values at all. What am I missing?

My view (UnitContract as strongly typed model):

...
<form class="pull-right" action="~/UnitDetails/EnableDisableUnit" method="POST">
        <input type="submit" class="k-button" value="Enable Unit"/>
    </form>

My controller action:

[HttpPost]
public ActionResult EnableDisableUnit(UnitContract model)
{
    var client = new UnitServiceClient();
    if (model.Disabled)
    {
        client.EnableUnit(model.Id);
    }
    else
    {
        client.DisableUnit(model.Id);
    }

    return RedirectToAction("Index", model.Id);
}

Sounds like you need to add the fields from your model to your form. Assuming your view accepts a UnitContract model, then something like this should work:

<form class="pull-right" action="~/UnitDetails/EnableDisableUnit" method="POST">
    @Html.HiddenFor(x => x.Id)
    @Html.HiddenFor(x => x.Disabled)
    <input type="submit" class="k-button" value="Enable Unit"/>
</form>

Now when you submit the form, it should submit the fields to your model.

The MVC framework will use the data from the form to create the model. As your form is essentially empty, there is no data to create the model from, so you get an object without any data populated.

The only data that is sent from the browser in the request when you post the form, is the data that is inside the form. You have to put the data for the properties in the model as fields in the form, so that there is something to populate the model with.

Look into using @Html.HiddenFor() . Put these in your form, and the data you want to see posted back to your controller should be there. For example, your form would look something like...

<form class="pull-right" action="~/UnitDetails/EnableDisableUnit" method="POST">
    @Html.HiddenFor(x => x.Id)
    @Html.HiddenFor(x => x.IsDisabled)
    <input type="submit" class="k-button" value="Enable Unit"/>
</form>

Let's say you have a model like this:

public class UnitContract
{
    public int Id { get; set; }
    public DateTime SignedOn { get; set; }
    public string UnitName { get; set; }
}

Your view would look something like this:

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

    <fieldset>
        <legend>UnitContract</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.SignedOn)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.SignedOn)
            @Html.ValidationMessageFor(model => model.SignedOn)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UnitName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UnitName)
            @Html.ValidationMessageFor(model => model.UnitName)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

In your controller:

    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Edit(UnitContract unitContract)
    {
        // do your business here .... unitContract.Id has a value at this point
        return View();
    }

Hope this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM