【问题标题】:Unexpected Html.EditorFor behavior in ASP.NET MVC 2ASP.NET MVC 2 中的意外 Html.EditorFor 行为
【发布时间】:2010-03-31 01:17:02
【问题描述】:

我从 Html.EditorFor() 得到一些意外行为。

我有这个控制器:

[HandleError]
public class HomeController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Lister()
    {
        string[] values = { "Hello", "world", "!!!" };

        return View(values);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Lister(string[] values)
    {
        string[] newValues = { "Some", "other", "values" };

        return View(newValues);
    }
}

这是我的观点,适用于这两种情况:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<string[]>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Lister
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Lister</h2>

    <% using (Html.BeginForm()) { %>
        <% foreach (string value in Model) { %>
            <%= value %><br />
        <% } %>
        <%= Html.EditorForModel() %>
        <input type="submit" value="Append Dashes" />
    <% } %>

</asp:Content>

问题在于,当从视图返回回帖时,它会触发正确的操作,但文本框仍然显示原始的 hello world 数据,而 foreach 循环输出新值强>。感觉就像 ASP.NET 中的某些东西通过更新文本框覆盖了我的模型值,它们只是显示相同的旧值。

我在尝试使用 IEnumerable 学习 EditorFor 时发现了这个问题。

【问题讨论】:

    标签: c# asp.net-mvc-2 editorfor


    【解决方案1】:

    这不是问题,这是正常行为。所有助手都是这样工作的。他们首先查看发布的值,然后查看模型以执行绑定。也就是说,即使您在控制器操作中修改模型,它们也会使用初始发布的值。

    相关问题:

    【讨论】:

    • 嗯,这对我来说有点烦人,你有没有相关文档的链接?
    • 我不确定这是否是记录在案的行为。我提供的第二个链接是来自 Phil Haack 的答案,应该绰绰有余:-)
    猜你喜欢
    • 2011-03-20
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多