Unexpected Html.EditorFor behavior in ASP.NET MVC 2

Posted by NickLarsen on Stack Overflow See other posts from Stack Overflow or by NickLarsen
Published on 2010-03-30T17:06:01Z Indexed on 2010/03/30 17:13 UTC
Read the original article Hit count: 547

Filed under:
|
|

I am getting some unexpected behavior from 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. 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.

I found this issue while trying to learn EditorFor with an IEnumerable.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc-2