ASP.NET MVC2 InputHelper和List.Insert()的奇怪行为 [英] ASP.NET MVC2 InputHelper and List.Insert() strange behavior

查看:112
本文介绍了ASP.NET MVC2 InputHelper和List.Insert()的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解这一点...

简化code以下应该插入当前时间的第二个值的新的表单字段。

相反,即使列表操作正确执行(如控制器调试输出内和可核查),这个视图渲染一个额外的元素,但似乎只是一个最后的字段的值的副本(前提交) 。这可以通过更改提交前场进行验证 - 价值观坚持和新元素是提交最终值的副本

这是真正的厨师给我的面条的部分是,如果我从平凡的Insert()改变操作的添加(),添加()按预期工作!

使用这个例子型号:

 公共类MyViewData
{
  清单<串GT; StringData是=新名单<串GT;();
  公开名单<串GT; StringData是{{返回StringData是; }}
}

和这个控制器:

 公共类的TestController:控制器
{
  公众的ActionResult指数()
  {
    返回查看(新MyViewData());
  }  [HttpPost]
  公众的ActionResult指数(MyViewData数据)
  {
    data.StringData.Insert(0,DateTime.Now.Second.ToString());    返回查看(数据);
  }
}

和这样的观点:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage< Forms.Controllers.MyViewData>中%GT;< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
    测试
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
    <%
        System.Diagnostics.Debug.WriteLine(---);
        对于(VAR I = 0; I< Model.StringData.Count;我++)
            System.Diagnostics.Debug.WriteLine({0},Model.StringData [I]);
    %GT;    &下;使用%(Html.BeginForm()){%GT;        <%为(VAR I = 0; I< Model.StringData.Count;我++){%GT;            <%:Html.TextBoxFor(型号=> model.StringData [I])%GT;< / BR>        <%}%GT;        < D​​IV><输入类型=提交值=做吧/>< / DIV>    <%}%GT;
< / ASP:内容>


解决方案

这是标准的HTML佣工的正常行为,是由设计。当渲染输入字段,他们将首先看一下请求HTTP POST的价值只有在与ViewData的视图模型后。这基本上意味着,您不能的变化在你的控制器动作发布的值。

所以,如果你有一个输入字段的表单:

 <%= Html.TextBoxFox(X => x.Id)%GT;

这是张贴到下面的操作

  [HttpPost]
公众的ActionResult指数(为MyModel模型)
{
    model.Id =一些新的价值;
    返回查看(模型);
}

渲染视图背面的HTML辅助将使用贴值时

。您既可以编写自定义HTML辅助,没有工作适合你或手动处理它(绝对不推荐,但备案):

 <输入类型=文本名称=StringData是[<%= I%]的计算值VALUE =<%= Model.StringData [I]%GT ; />

I cannot make sense of this...

The simplified code below OUGHT to insert a new form field of the current time's second value.

Instead, even though the list manipulation happens correctly (as verifiable within the controller and in the debug output), the View does render an additional element, but seems to be just a copy of the final field's value (prior to submit). This can be verified by changing a field prior to submit - the values stick and the new element is a duplicate of the submitted final value.

The part that really cooks my noodle is that if I trivially change the operation from an Insert() to an Add(), the Add() works as expected!!

Using this example Model:

public class MyViewData
{
  List<string> stringData = new List<string>();
  public List<string> StringData { get { return stringData; } }
}

And this Controller:

public class TestController : Controller
{
  public ActionResult Index()
  {
    return View(new MyViewData());
  }

  [HttpPost]
  public ActionResult Index(MyViewData data)
  {
    data.StringData.Insert(0, DateTime.Now.Second.ToString());

    return View(data);
  }
}

And this View:

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

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Test
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%      
        System.Diagnostics.Debug.WriteLine("---");
        for (var i = 0; i < Model.StringData.Count; i++)
            System.Diagnostics.Debug.WriteLine("{0}", Model.StringData[i]);
    %>

    <% using (Html.BeginForm()) { %>

        <% for (var i = 0; i < Model.StringData.Count; i++) { %>

            <%: Html.TextBoxFor(model => model.StringData[i])%></br>

        <% } %>

        <div><input type="submit" value="Do it" /></div>

    <% } %>
</asp:Content>

解决方案

This is the normal behavior of standard HTML helpers and is by design. When rendering the input field they will first look at the request POSTed values and only after in the ViewData and view model. This basically means that you cannot change POSted values in your controller action.

So if you have a form with an input field:

<%= Html.TextBoxFox(x => x.Id) %>

which is posted to the following action

[HttpPost]
public ActionResult Index(MyModel model)
{
    model.Id = "some new value";
    return View(model);
}

when rendering the view back the html helper will use the posted value. You could either write a custom html helper that does the job for you or handle it manually (absolutely not recommended but for the record):

<input type="text" name="StringData[<%= i %>]" value="<%= Model.StringData[i] %>" />

这篇关于ASP.NET MVC2 InputHelper和List.Insert()的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆