使用 EditorFor 时丢失帖子数据
问题:
当我提交表单时,我丢失了输入到地址编辑模板中的所有信息。
我有以下模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提交表单的操作必须如下所示:
或者,如果您尝试直接绑定到两个地址,则需要设置正确的前缀:
The action you are submitting the form to must look like this:
Or if you are trying to bind directly to the two addresses you need to set the correct prefix: