ASP.Net MVC的ModelState / Html.TextBox回发的问题 [英] ASP.Net MVC ModelState / Html.TextBox postback issue

查看:148
本文介绍了ASP.Net MVC的ModelState / Html.TextBox回发的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在表单中我试图张贴此起彼伏。在形式不验证的情况下,我正在打电话的标准路由 ModelState.AddModelError()然后返回一个视图的结果。

问题是,在HTML *佣工应该渲染时拿起贴价值,我注意到我的文本字段只能这样做,如果我将它们包括在回传动作的参数列表,这不该 T为需要看到,因为某些形式有太多的领域要列出所有这些作为参数。<​​/ p>

我的行动code大致是:

 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult EditDataDefinition(长?ID,字符串名称)
{
    VAR dataDefinition = ...    //做一些验证的东西
    如果(!ModelState.IsValid)
    {
        //手动设置通过ViewData的复选框场看到,因为这仍然是MC 1.0不起作用:P
        // ...
        返回查看(dataDefinition);
    }}

现在,dataDefinition(这是一个LINQ到SQL实体)具有一字段的 MinVolume 的,在由该行视图中处理:

 最低:LT;%= Html.TextBox(MinVolume,空,新的{大小= 5})%GT;

然而,当视图是一个失败的ModelState验证后呈现,键入到它,我们张贴的原始页上的价值并不preserved除非我把它作为在回发方法的参数。从字面上看,我可以做这个解决问题:

 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult EditDataDefinition(长?ID,字符串名称,字符串minVolume)

由于某种原因,这将迫使该字段值是preserved。这似乎是愚蠢的我,因为我的表已经不仅仅是这样更多的价值,我不应该添加一个参数只是场。

任何想法?


解决方案

难道说你的code:

 &LT;%= Html.TextBox(MinVolume,空,新的{大小= 5})%GT;

..有默认值参数?也许如果你修改 Model.MinVolume 它会持续的价值。像这样的:

 &LT;%= Html.TextBox(MinVolume,Model.MinVolume,新{大小= 5})%GT;

我不知道,如果你的动作返回模型寿值 MinVolume 。如果这样做,在上述应该工作。否则,你可能需要稍微重构行动。

I have an issue cropping up in a form I'm trying to post. In the scenario where the form doesn't validate, I'm taking the standard route of calling ModelState.AddModelError() then returning a View result.

The thing is, the HTML.* helpers are supposed to pick up the posted value when rendering and I'm noticing that my text fields ONLY do so if I include them in the parameter list of the postback action, which shouldn't be required seeing as some forms have way too many fields to want to list them all as parameters.

My action code is roughly:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(long? id, string name)
{
    var dataDefinition = ...

    // do some validation stuff
    if (!ModelState.IsValid)
    {
        // manually set checkbox fields via ViewData seeing as this STILL doesn't work in MC 1.0 :P
        // ...
        return View(dataDefinition);
    }

}

Now, dataDefinition (which is a LINQ to SQL entity) has a field MinVolume, is handled in the view by this line:

Minimum: <%= Html.TextBox("MinVolume", null, new { size = 5 })%>

Yet when the view is rendered after a failed ModelState validation, the value typed into it on the original page we posted is not preserved UNLESS I include it as a parameter in the postback method. Literally, I can "solve the problem" by doing this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(long? id, string name, string minVolume)

For some reason that will force the field value to be preserved. This seems stupid to me because my form has way more values than just that and I shouldn't have to add a parameter for just that field.

Any ideas?

解决方案

Could it be that your code:

<%= Html.TextBox("MinVolume", null, new { size = 5 })%>

..has the null for the default value param? Maybe if you change the null to Model.MinVolume it will persist the value. Like this:

<%= Html.TextBox("MinVolume", Model.MinVolume, new { size = 5 })%>

I'm not sure if your action returns the value MinVolume in the model tho. If it does, the above should work. Else, you may need to refactor the action slightly.

这篇关于ASP.Net MVC的ModelState / Html.TextBox回发的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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