简体   繁体   English

为什么 SelectList SelectedValue 在 HttpGet 上有效,但在 HttpPost 上无效?

[英]Why does SelectList SelectedValue work on HttpGet but not on HttpPost?

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.使用 MVC3,我发现设置 SelectList 的选定值可以正确地在 HttpGet 上呈现视图,但无法在 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.在将 Model 转发到 HttpPost 上的视图之前,我已经检查了它们并且它们正在正确更新,看来视图没有正确呈现所选标签。

On HttpPost, the <select> is rendered exactly as it existed after any edits but before submission of the form.在 HttpPost 上, <select>在任何编辑之后但在提交表单之前完全呈现。 The m.SelectedWidgetId = 2; m.SelectedWidgetId = 2; in the HttpPost method is executed, updates the model, but is not reflected in the View.在HttpPost方法执行后,更新了model,但没有反映在View中。

What am I missing here?我在这里想念什么?

Model: 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; Controller 方法;

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.发生这种情况是因为 HTML 助手总是在呈现值时使用请求中的值,然后再使用 model 中的值。 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:这基本上意味着,如果您想修改 POST 操作中的某些值,您需要首先将其从 model state 中删除:

[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.或者助手将简单地忽略您手动设置的值并使用用户发布的值。

in asp.net mvc the selectedvalue of selectList is overridden by value of the property for which dropdown is created when we use strongly typed helper ie在 asp.net mvc 中,selectList 的 selectedvalue 被我们使用强类型帮助器时创建下拉列表的属性的值覆盖,即

<%:Html.DropDownListFor(x=>x.SelectedWidgetID, ---,----)%>

in this case value of Model.selectedwidgetID will override the value that is set in在这种情况下,Model.selectedwidgetID 的值将覆盖在

new SelectList(Widgets.ToList(), "Id", "Name", SelectedWidgetId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM