开发者

Selectbox not set even when view object is set?

Hi,

I got help with this problem a couple of days ago :

Strange problem with ASP.NET MVC DropDownFor

The solution i got worked, but now I have runned in to a simular problem with the same view and action.

This time I got the following in a partial view that is loaded in to the main view :

<%: Html.DropDownListFor(model => model.ST1, Model.ShowAdTypeList1, new { @class = "dd2", @style = "width:80px;" })%>

This will be rendered like this :

<select name="ALS.ST1" id="ALS_ST1">
<option value="0">Alla</option>
<option value="1">Privata</option>
<option value="2">Företag</option>
</select>

I have the following code at the end of the action

    data.ALS.ST1 = 2;
    ModelState.Clear();
    return View(data);

The problem is that the dropdown is always selecting 0? When choosing value 2 manually in the dropdown the data.ALS.ST1 will be set to 2 when entering the action.

Why is it not setting the dropdown to value 2?

Edit1:

This is the only javascript that works with the ALS_ST1 on the entire page :

$('#ALS_ST1').change(function () {
            if (IsNotDblClick(this.id)) {
                document.forms['list_ad'].submit();
            }
            else
                return false;
        });

Edit2

The Model.ShowAdTypeList1 is a of the type SelectList and do not have a selection. The DropDownFore is supose to set the ST1 value 开发者_开发百科to selected.

EDIT3

Pleas note that this partial view is in fact a template : http://weblogs.asp.net/rashid/archive/2009/11/27/extending-asp-net-mvc-2-templates.aspx.


Try rendering the dropdownlist like this:

<%= Html.DropDownListFor(
    x => x.ST1, 
    new SelectList(Model.ShowAdTypeList1, "Value", "Text", Model.ST1), 
    new { @class = "dd2", @style = "width:80px;" }
) %>

This assumes that the ShowAdTypeList1 property is an IEnumerable<SelectListItem>. If it is not you might need to adjust the Value and Text arguments.

Also instead of clearing the entire modelstate I would recommend you reset only the properties that you are modifying inside your POST action:

ModelState.Remove("ALS.ST1");
data.ALS.ST1 = 2;
return View(data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜