Why does SelectList SelectedValue work on HttpGet but not on HttpPost?

随声附和 提交于 2020-01-15 06:10:17

问题


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);
}

回答1:


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.




回答2:


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 i.e

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

in this case value of Model.selectedwidgetID will override the value that is set in

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


来源:https://stackoverflow.com/questions/5893185/why-does-selectlist-selectedvalue-work-on-httpget-but-not-on-httppost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!