即使设置了视图对象,选择框也未设置?

发布于 2024-11-28 10:40:30 字数 1562 浏览 0 评论 0原文

嗨,

几天前我得到了有关此问题的帮助:

ASP.NET MVC DropDownFor 的奇怪问题

我已经解决了这个问题,但现在我遇到了具有相同视图和操作的类似问题。

这次我在加载到主视图中的部分视图中得到以下内容:

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

这将呈现如下:

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

我在操作末尾有以下代码

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

问题是下拉列表始终选择 0?在下拉列表中手动选择值 2 时,输入操作时 data.ALS.ST1 将设置为 2。

为什么不将下拉菜单设置为值 2?

Edit1:

这是整个页面上与 ALS_ST1 一起使用的唯一 javascript:

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

Edit2

Model.ShowAdTypeList1 是 SelectList 类型,没有选择。 DropDownFore 应该将 ST1 值设置为选中。

EDIT3

请注意,此部分视图实际上是一个模板:http://weblogs.asp.net/rashid/archive/2009/11/27/extending-asp-net-mvc-2-templates.aspx

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2024-12-05 10:40:30

尝试像这样渲染下拉列表:

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

这里假设 ShowAdTypeList1 属性是 IEnumerable。如果不是,您可能需要调整 ValueText 参数。

另外,我建议您只重置您在 POST 操作中修改的属性,而不是清除整个模型状态:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文