简体   繁体   English

Post后,Asp.Net MVC EditorTemplate模型丢失了

[英]Asp.Net MVC EditorTemplate Model is lost after Post

I have a controller with two simple Methods: 我有一个控制器有两个简单的方法:

UserController Methods: UserController方法:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Details(string id)
{
 User user = UserRepo.UserByID(id);

 return View(user);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Details(User user)
{
 return View(user);
}

Then there is one simple view for displaying the details: 然后有一个简单的视图来显示细节:

<% using (Html.BeginForm("Details", "User", FormMethod.Post))
   {%>
 <fieldset>
  <legend>Userinfo</legend>
  <%= Html.EditorFor(m => m.Name, "LabelTextBoxValidation")%>
  <%= Html.EditorFor(m => m.Email, "LabelTextBoxValidation")%>
  <%= Html.EditorFor(m => m.Telephone, "LabelTextBoxValidation")%>
 </fieldset>
 <input type="submit" id="btnChange" value="Change" />
<% } %>

As you can see, I use an editor template "LabelTextBoxValidation": 如您所见,我使用编辑器模板“LabelTextBoxValidation”:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.Label("") %>
<%= Html.TextBox(Model,Model)%>
<%= Html.ValidationMessage("")%>

Showing user information is no problem. 显示用户信息没问题。 The view renders perfectly user details. 该视图呈现完美的用户详细信息。 When I submit the form, the object user is lost. 当我提交表单时,对象用户将丢失。 I debugged on the row "return View(User);" 我调试了行“return View(User);” in the Post Details method, the user object is filled with nullable values. 在Post Details方法中,用户对象填充了可空值。 If I dont use the editor template, the user object is filled with correct data. 如果我不使用编辑器模板,则用户对象将填充正确的数据。 So there has to be something wrong with the editor template, but can't figure out what it is. 因此编辑器模板必定存在问题,但无法弄清楚它是什么。 Suggestions? 建议?

I would re-architect a little bit - change your LabelTextBoxValidation editor to an Html helper, and then make one EditorTemplate for your data model. 我会重新构建一下 - 将LabelTextBoxValidation编辑器更改为Html帮助程序,然后为您的数据模型创建一个EditorTemplate。 That way you could do something like this: 这样你就可以这样做:

<% using (Html.BeginForm("Details", "User", FormMethod.Post))
{%>
  <fieldset>
   <legend>Userinfo</legend>
   <% Html.EditorFor(m => m); %>
  </fieldset>
  <input type="submit" id="btnChange" value="Change" />
<% } %>

And your editor template would be something like: 你的编辑器模板将是这样的:

<%= Html.ValidatedTextBoxFor(m => m.Name); %>
<%= Html.ValidatedTextBoxFor(m => m.Email); %>
<%= Html.ValidatedTextBoxFor(m => m.Telephone); %>

where ValidatedTextBoxFor is your new html helper. ValidatedTextBoxFor是你的新html助手。 To make that, it would be fairly easy: 要做到这一点,这将是相当容易的:

public static MvcHtmlString ValidatedTextBoxFor<T>(this HtmlHelper helper, Expression thingy)
{
     // Some pseudo code, Visual Studio isn't in front of me right now
     return helper.LabelFor(thingy) + helper.TextBoxFor(thingy) + helper.ValidationMessageFor(thingy);
}

That should set the names of the form fields right I believe, as that seems like the source of the problem. 这应该设置表格字段的名称,我相信,因为这似乎是问题的根源。

EDIT : Here is the code that should help you out: 编辑 :这是应该帮助你的代码:

public static MvcHtmlString ValidatedTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    return MvcHtmlString.Create(
           html.LabelFor(expression).ToString() +
           html.TextBoxFor(expression).ToString() +
           html.ValidationMessageFor(expression).ToString()
           );
}

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

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