简体   繁体   English

为什么我的模型未通过表单发布作为参数传递

[英]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. 我有一个带有强类型模型(UnitContract)的视图,该模型是从Web服务中获取的,该服务具有一组值。 In my action I'm trying to fetch int ID and bool Disabled fields that exists in my model. 在我的行动中,我试图获取模型中存在的int ID和bool Disabled字段。 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): 我的观点(UnitContract是强类型模型):

...
<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: 假设您的视图接受UnitContract模型,则类似这样的方法应该起作用:

<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. MVC框架将使用表单中的数据来创建模型。 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() . 研究使用@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. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的参数值未在QueryString中传递 - why is my parameter value not being passed in QueryString 为什么我的委托作为参数传递给方法时不起作用? - Why does my delegate not work when passed in as a parameter to a method? 为什么我的隐藏输入没有在模型类中传递? - Why are my hidden input not being passed in the model class? 为什么我发布的模型会丢失下拉列表项? - Why my model in post lose dropdownlist items? 模型作为 null 传递给 post 方法 - model passed as null to the post method 无法让Controller建立模型表单查询和传入的参数-多个模型获取数据 - Cannot get Controller to build model form query and parameter passed in - multiple models to get data 模型没有在post方法中传递,后者应该从更改的表单输入值更新实体 - Model not being passed in post method which should update entities from changed form input values 为什么我的表单没有在视图模型中填充列表? - Why is my form not populating my list on my view model? ASP.NET Core MVC添加的Edit()动作方法(POST)脚手架为什么ID参数传了两次? - Why is the ID parameter passed twice in the Edit() action method (POST) scaffold added by ASP.NET Core MVC? 传递给后期控制器的参数始终为null - Parameter passed to post controller is always null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM