MVC2 TextBoxFor值不更新后提交? [英] MVC2 TextBoxFor value not updating after submit?

查看:90
本文介绍了MVC2 TextBoxFor值不更新后提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常奇怪的行为,我已经设置了一些演示code,试图弄清楚发生了什么事情。

This is a really strange behavior, and I've set up some demo code to try to figure out what's going on.

基本上有一个两个动作和单一视图。第一个动作将一个空的模型认为,部分动作临危模型,改变它的内容并把它发送回同样的观点。

Basically have a a two actions and a single view. The first action sends an empty model to the view, the section action recieves the model, alters its contents and sends it back to the same view.

该wierdness是,在视图中,模型似乎有它的更新值,但是当我做一个Html.TextBoxFor(X => x.PropertyNameHere),它呈现与不变值一个文本框在里面。

The wierdness is, in the view, the Model seems to have the updated values in it, but when I do an Html.TextBoxFor(x => x.PropertyNameHere) it renders a textbox with the unaltered value in it.

哈哈......我先上厕所幽默道歉,但它使日子变得太无聊了。 ;)

lol... I apologize in advance for the toilet humor, but it keeps the day from getting too boring. ;)

有没有人有任何想法是怎么回事?为什么TextBoxFor的把旧值中值的输出属性?

这里的code复制:

/Views/Demo/Index.aspx

/Views/Demo/Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<TestWeb.DemoModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Demo</title>
</head>
<body>
    <div>
      <%using (Html.BeginForm("DemoSubmit", "Admin", FormMethod.Post)) { %>
        Foo: <%=Html.TextBoxFor(x => x.Foo)%> <%:Model.Foo %><br />
        Bar: <%=Html.TextBoxFor(x => x.Bar) %> <%:Model.Bar %><br />
        PoopSmith: <%=Html.TextBoxFor(x => x.PoopSmith) %> <%:Model.PoopSmith %><br />
        <button type="submit">Submit</button>
      <%} %>
    </div>
</body>
</html>

DemoModel.cs

DemoModel.cs

namespace TestWeb {
    public class DemoModel {
        public string Foo { get; set; }
        public int Bar { get; set; }
        public string PoopSmith { get; set; }
    }
}

DemoController.cs

DemoController.cs

public class AdminController : Controller {

        public ActionResult Index() {
            var m = new DemoModel();
            return View(m);
        }

        public ActionResult DemoSubmit(DemoModel demo) {
            demo.Foo += "!!!";
            demo.Bar++;
            demo.PoopSmith += " has pooped.";
            return View("Index", demo);
        }
}

和这里的离奇输出:

推荐答案

默认HTML辅助试图重新显示的发布给他们的数据。
他们首先从发布的数据使用的值,如果没有发布的数据,请他们从模型中的数据。

Default Html helper try to redisplay the data that is posted to them. They first use the value from posted data and if no posted data is available they take the data from the Model.

这是不是你想要什么明显,但仍是最常见的用法:您收到GET请求后显示formfields一些数据。您发布到更新的动作。如果有错误要与价值观重新显示表单您输入仍然可用。

This is not what you want obviously, but still the most common usage: You display some data in formfields after receiving a get request. You post to an Update action. If you have errors you want to redisplay the form with the values you entered still available.

我见过一些人围着这让(我想通过写ModelState中),但我的选择是永远,如果他们不帮我,不使用默认的帮手。这就是为隐藏字段尤其如此:大部分人感到困惑时,他们的值设置为隐藏字段,但就是真的使用的值是从岗位。至少有一个问题关于它每隔一天SO: - )

I have seen some people getting around this (I think by writing to ModelState), but my choice was always to not use the default helpers if they dont help me. Thats especially true for hidden fields: Most people get confused when they set a value to a hidden field but the value that is realy used is from the post. At least there is a question every other day about it on SO :-)

忘记大多数人和大家替换它。

Forget the "Most people" and replace it with "Everybody".

<一个href=\"http://stackoverflow.com/questions/2019131/asp-net-mvc-hidden-field-value-does-not-get-rendered-using-htmlhelper-hidden\">http://stackoverflow.com/questions/2019131/asp-net-mvc-hidden-field-value-does-not-get-rendered-using-htmlhelper-hidden

<一个href=\"http://blog.johnwest.com/post/ASPNET-MVC-Hidden-Form-Field-Bug.aspx\">http://blog.johnwest.com/post/ASPNET-MVC-Hidden-Form-Field-Bug.aspx

<一个href=\"http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx?utm_medium=Twitter&utm_source=Shared\">http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx?utm_medium=Twitter&utm_source=Shared

更新
哦,我发现从今天另外一个(你并不孤单):

UPDATE Oh I found another one from today (You are not alone):

<一个href=\"http://stackoverflow.com/questions/2924057/how-to-update-textbox-value\">http://stackoverflow.com/questions/2924057/how-to-update-textbox-value

这篇关于MVC2 TextBoxFor值不更新后提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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