使用 EditorFor 时丢失帖子数据

发布于 2024-11-01 07:04:22 字数 2289 浏览 0 评论 0原文

问题:

当我提交表单时,我丢失了输入到地址编辑模板中的所有信息。

我有以下模型:

public class MoveRecord
{
    public Address StartPoint { get; set; }
    public Address EndPoint { get; set; }
}

其中 Address 定义为:

public class Address
{
    public String City { get; set;}
    public String State { get; set; }
    public String Line1{ get; set; }
    public String PostalCode { get; set; }
}

这是我的操作方法:

[HttpPost]
public ActionResult Edit(MoveRecord model)
{
    if (ModelState.IsValid)
    {
        //Save info
    }

    return View(model);
}

我的编辑视图使用:

using (Html.BeginForm("Edit", "Move", FormMethod.Post))
{
    @Html.EditorFor(m => m.StartPoint);
    @Html.EditorFor(m => m.EndPoint);
    <input type="submit" value="Save" />
}

我的编辑模板是:

<table class="form address">
    <tbody>
        <tr>
            <th style="width: 200px;">
                @Html.LabelFor(m => m.Line1):
            </th>
            <td>
                @Html.TextBoxFor(m => m.Line1, new { style = "width: 300px;" })
            </td>
        </tr>
        <tr id="zip">
            <th>
                @Html.LabelFor(m => m.PostalCode):
            </th>
            <td>
                @Html.TextBoxFor(m => m.PostalCode, new { style = "width: 150px;" })
            </td>
        </tr>
        <tr id="city">
            <th>
                @Html.LabelFor(m => m.City):
            </th>
            <td>
                @Html.TextBoxFor(m => m.City, new { style = "width: 150px;" })
            </td>
        </tr>
        <tr id="state">
            <th>
                @Html.LabelFor(m => m.StateProvince):
            </th>
            <td> 
               @Html.TextBoxFor(m => m.StateProvince, new { style = "width: 150px;" })
            </td>
        </tr>
    </tbody>
</table>

一切都渲染得很好,但是当我提交表单时,我在操作方法中获得的模型不会包含输入地址字段的任何信息。如果我将所有内容移动到同一个视图中,那么它就可以正常工作,但我希望能够使用编辑器模板来使我的视图易于阅读。 如何从编辑器模板中获取正确绑定到模型的数据?

编辑:发布我的操作方法

The Problem:

When I submit the form I am losing any information entered into the edit templates for the addresses.

I have the following model:

public class MoveRecord
{
    public Address StartPoint { get; set; }
    public Address EndPoint { get; set; }
}

Where Address is defined as:

public class Address
{
    public String City { get; set;}
    public String State { get; set; }
    public String Line1{ get; set; }
    public String PostalCode { get; set; }
}

Here is my action method:

[HttpPost]
public ActionResult Edit(MoveRecord model)
{
    if (ModelState.IsValid)
    {
        //Save info
    }

    return View(model);
}

My edit view uses:

using (Html.BeginForm("Edit", "Move", FormMethod.Post))
{
    @Html.EditorFor(m => m.StartPoint);
    @Html.EditorFor(m => m.EndPoint);
    <input type="submit" value="Save" />
}

My edit template is:

<table class="form address">
    <tbody>
        <tr>
            <th style="width: 200px;">
                @Html.LabelFor(m => m.Line1):
            </th>
            <td>
                @Html.TextBoxFor(m => m.Line1, new { style = "width: 300px;" })
            </td>
        </tr>
        <tr id="zip">
            <th>
                @Html.LabelFor(m => m.PostalCode):
            </th>
            <td>
                @Html.TextBoxFor(m => m.PostalCode, new { style = "width: 150px;" })
            </td>
        </tr>
        <tr id="city">
            <th>
                @Html.LabelFor(m => m.City):
            </th>
            <td>
                @Html.TextBoxFor(m => m.City, new { style = "width: 150px;" })
            </td>
        </tr>
        <tr id="state">
            <th>
                @Html.LabelFor(m => m.StateProvince):
            </th>
            <td> 
               @Html.TextBoxFor(m => m.StateProvince, new { style = "width: 150px;" })
            </td>
        </tr>
    </tbody>
</table>

Everything renders just fine, however when I submit the form the model that I get in the action method does not contain any information entered into the address field. If I move everything into the same view then it works just fine but what I would like is to be able to use the editor templates to keep my views easy to read. How can I get the data from the editor template correctly bound to the model?

Edit: Posted my action method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-11-08 07:04:22

您提交表单的操作必须如下所示:

[HttpPost]
public ActionResult Edit(MoveRecord model)
{
    // model.StartPoint and model.EndPoint should be correctly bound here.
    ...
}

或者,如果您尝试直接绑定到两个地址,则需要设置正确的前缀:

[HttpPost]
public ActionResult Edit(
    [Bind(Prefix = "StartPoint")] Address start, 
    [Bind(Prefix = "EndPoint")] Address end
)
{
    ...
}

The action you are submitting the form to must look like this:

[HttpPost]
public ActionResult Edit(MoveRecord model)
{
    // model.StartPoint and model.EndPoint should be correctly bound here.
    ...
}

Or if you are trying to bind directly to the two addresses you need to set the correct prefix:

[HttpPost]
public ActionResult Edit(
    [Bind(Prefix = "StartPoint")] Address start, 
    [Bind(Prefix = "EndPoint")] Address end
)
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文