简体   繁体   English

ASP.NET MVC 2 中意外的 Html.EditorFor 行为

[英]Unexpected Html.EditorFor behavior in ASP.NET MVC 2

I am getting some unexpected behavior from Html.EditorFor().我从 Html.EditorFor() 得到了一些意想不到的行为。

I have this controller:我有这个控制器:

[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);
    }
}

And this is my view which is intended to work for both of these:这是我的观点,旨在为这两个工作:

<%@ 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>

And the problem is that when the post back is made from the view, it hits the correct action, but the text boxes still show the original hello world data while the foreach loop outputs the new values .问题是,当从视图回发时,它会执行正确的操作,但文本框仍然显示原始的 hello world 数据,而foreach循环输出新值 It feels like something in ASP.NET is overriding my model values from updating the text boxes and they are just displaying the same old values.感觉 ASP.NET 中的某些东西正在通过更新文本框覆盖我的模型值,而它们只是显示相同的旧值。

I found this issue while trying to learn EditorFor with an IEnumerable.我在尝试使用 IEnumerable 学习 EditorFor 时发现了这个问题。

This is not a problem, it is the normal behavior.这不是问题,这是正常行为。 All helpers work that way.所有的助手都是这样工作的。 They first look at posted values and then the model in order to perform the binding.他们首先查看发布的值,然后查看模型以执行绑定。 That is to say even if you modify the model in your controller action, they will use the initial posted values.也就是说,即使您在控制器操作中修改模型,它们也会使用初始发布的值。

Related questions:相关问题:

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

相关问题 ASP.NET MVC 2 - Html.Editor对于可以为空的类型? - ASP.NET MVC 2 - Html.EditorFor a nullable type? ASP.NET MVC3 Html.EditorFor()问题 - ASP.NET MVC3 Html.EditorFor() problem 如何使 html.editor 在 asp.net mvc 2 中不可见? - how to make html.editorfor unvisible in asp.net mvc 2? 是否可以从 ASP.NET MVC 中的 @Html.EditorFor 中删除或隐藏最后一个条目的建议 - Is it possible to remove or hide suggestions of last entries from @Html.EditorFor in ASP.NET MVC 如何在ASP.net MVC 5中使用@ Html.EditorFor自动填充表单中的字段? - How to auto fill fields in a form using @Html.EditorFor in ASP.net MVC 5? ASP.NET MVC @ Html.EditorFor(模型=&gt;模型。属性)但是 - ASP.NET MVC @Html.EditorFor(model => model.property) But ASP.NET MVC 2:如何将Html.EditorFor用于自定义模型? - ASP.NET MVC 2: How do you use Html.EditorFor for custom models? 在ASP.Net MVC3中为特定数据类型模板Html.EditorFor吗? - Templating Html.EditorFor for specific datatype in ASP.Net MVC3? 不使用Html.EditorFor()asp.net mvc创建模型的引用时出现的问题 - Issue when creating a reference of a Model without using Html.EditorFor() asp.net mvc 在ASP.NET MVC Razor View中将class属性设置为Html.EditorFor - Set the class attribute to Html.EditorFor in ASP.NET MVC Razor View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM