为什么的SelectList的SelectedValue HTTPGET上而不是在HttpPost工作? [英] Why does SelectList SelectedValue work on HttpGet but not on HttpPost?

查看:97
本文介绍了为什么的SelectList的SelectedValue HTTPGET上而不是在HttpPost工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC3我发现设置的SelectList的选定值正确呈现在一个HTTPGET的观点,但未能正确地呈现在HttpPost。我已检查了示范它们转发到HttpPost查看之前和他们正确进行更新,它只是似乎该视图不能正确地显示选择的标签。

在HttpPost,在<选择> 精确地呈现为任何修改之后,但提交表单之前就已经存在。在 m.SelectedWidgetId = 2; 在执行方法HttpPost,更新模型,但没有反映在视图

我是什么在这里失踪?

型号:

 公共类WidgetModel
{
    私人小部件[] {小工具
        得到
        {
            返回全新的Widget [] {
                全新的Widget {ID = 1,名称=项目1},
                全新的Widget {ID = 2,名称=项目2},
                全新的Widget {ID = 3,名称=项目3}
            };
        }
    }
    公众的SelectList的widgetList
    {
        得到
        {
            返回新的SelectList(Widgets.ToList(),ID,姓名,SelectedWidgetId);
        }
    }
    公众诠释SelectedWidgetId {搞定;组; }
}

查看:

  @model thisProject.Models.WidgetModel@using(Html.BeginForm())
{
    @ Html.DropDownListFor(M = GT; m.SelectedWidgetId,Model.WidgetList,选择...);
    <输入类型=提交/>
}

控制器方法;

 公共ActionResult的小工具()
{
    变种M =新WidgetModel();
    m.SelectedWidgetId = 1;
    返回查看(米);
}
[HttpPost]
公众的ActionResult小工具(WidgetModel米)
{
    m.SelectedWidgetId = 2;
    返回查看(米);
}


解决方案

这是因为HTML辅助渲染一个值时,一定要使用在模型中的之前请求的值。这基本上意味着,如果你想在POST操作修改一些价值,你需要从模型状态首先除去它:

  [HttpPost]
公众的ActionResult小工具(WidgetModel米)
{
    ModelState.Remove(SelectedWidgetId);
    m.SelectedWidgetId = 2;
    返回查看(米);
}

或助手将简单地忽略手动设置的值,并使用由用户发布一个。

Using MVC3 I have found that setting a SelectList's selected value correctly renders the view on an HttpGet, but fails to render correctly on HttpPost. I have inspected the Model before they are forwarded to the View on HttpPost and they are correctly being updated, it just seems the View is not rendering the selected tag correctly.

On HttpPost, the <select> is rendered exactly as it existed after any edits but before submission of the form. The m.SelectedWidgetId = 2; in the HttpPost method is executed, updates the model, but is not reflected in the View.

What am I missing here?

Model:

public class WidgetModel
{
    private Widget[] Widgets {
        get
        {
            return new Widget[] { 
                new Widget { Id=1, Name="Item 1" },
                new Widget { Id=2, Name="Item 2" },
                new Widget { Id=3, Name="Item 3" }
            };
        }
    }
    public SelectList WidgetList
    {
        get
        {
            return new SelectList(Widgets.ToList(), "Id", "Name", SelectedWidgetId);
        }
    }
    public int SelectedWidgetId { get; set; }
}

View:

@model thisProject.Models.WidgetModel

@using (Html.BeginForm())
{
    @Html.DropDownListFor(m => m.SelectedWidgetId, Model.WidgetList, "Select...");      
    <input type='submit' />                                                                                    
}

Controller Methods;

public ActionResult Widget()
{
    var m = new WidgetModel();
    m.SelectedWidgetId = 1;
    return View(m);
}
[HttpPost]
public ActionResult Widget(WidgetModel m)
{
    m.SelectedWidgetId = 2;
    return View(m);
}

解决方案

That happens because HTML helpers always use the values in the request before the ones in the model when rendering a value. This basically means that if you want to modify some value in the POST action you need to remove it from the model state first:

[HttpPost]
public ActionResult Widget(WidgetModel m)
{
    ModelState.Remove("SelectedWidgetId");
    m.SelectedWidgetId = 2;
    return View(m);
}

or the helper will simply ignore the value you are manually setting and use the one that was POSTed by the user.

这篇关于为什么的SelectList的SelectedValue HTTPGET上而不是在HttpPost工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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