即使设置了视图对象,选择框也未设置?
嗨,
几天前我得到了有关此问题的帮助:
我已经解决了这个问题,但现在我遇到了具有相同视图和操作的类似问题。
这次我在加载到主视图中的部分视图中得到以下内容:
<%: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样渲染下拉列表:
这里假设
ShowAdTypeList1
属性是IEnumerable
。如果不是,您可能需要调整Value
和Text
参数。另外,我建议您只重置您在 POST 操作中修改的属性,而不是清除整个模型状态:
Try rendering the dropdownlist like this:
This assumes that the
ShowAdTypeList1
property is anIEnumerable<SelectListItem>
. If it is not you might need to adjust theValue
andText
arguments.Also instead of clearing the entire modelstate I would recommend you reset only the properties that you are modifying inside your POST action: