Asp.net mvc 3 未绑定到模型属性mvc3的Dropdownlist

Asp.net mvc 3 未绑定到模型属性mvc3的Dropdownlist,asp.net-mvc-3,Asp.net Mvc 3,MVC3 DropDownList用于不将模型属性绑定到选定值 这是我的看法 @{var items = new List<SelectListItem>(){ new SelectListItem {Text = "2", Value = "2", Selected = true}, new SelectListItem {Text = "3", Value = "3",

MVC3 DropDownList用于不将模型属性绑定到选定值 这是我的看法

@{var items = new List<SelectListItem>(){
                            new SelectListItem {Text = "2", Value = "2", Selected = true},
                            new SelectListItem {Text = "3", Value = "3", Selected = false},
                            new SelectListItem {Text = "4", Value = "4", Selected = false},
                            new SelectListItem {Text = "5", Value = "5", Selected = false}
                        };

        }   
@Html.DropDownListFor(x => x.InvoiceItem.Count, new SelectList(items, "Value", "Text"))
@{var items=新列表(){
新建SelectListItem{Text=“2”,Value=“2”,Selected=true},
新建SelectListItem{Text=“3”,Value=“3”,Selected=false},
新建SelectListItem{Text=“4”,Value=“4”,Selected=false},
新建SelectListItem{Text=“5”,Value=“5”,Selected=false}
};
}   
@DropDownListFor(x=>x.InvoiceItem.Count,新选择列表(项,“值”,“文本”))
InvoiceModel有一个名为InvoiceItem类的属性,该属性还具有int类型的属性计数。 count属性始终为0,并且不会更新为从下拉列表中选择的值

请帮忙,我已经在这上面花了好几个小时了。多谢各位


谢谢你的回复。但我仍然有这个问题

我曾经 @DropDownListFor(x=>x.InvoiceItem.Count,新选择列表(项目,“值”,“文本”,2))

还尝试了@Html.DropDownListFor(x=>x.InvoiceItem.Count,新的SelectList(项目,“值”、“文本”、“2”))

Count属性始终为0。我在这里遗漏了什么。

使用:

new SelectList(items, "value", "text", selectedvalue);
例:

@{var items=新列表(){
新建SelectListItem{Text=“2”,Value=“2”},
新建SelectListItem{Text=“3”,Value=“3”},
新建SelectListItem{Text=“4”,Value=“4”},
新建SelectListItem{Text=“5”,Value=“5”}
};
}   
@DropDownListFor(x=>x.InvoiceItem.Count,新选择列表(项目,“值”,“文本”,2))

这是因为Model.InvoiceItem.Count的当前值(DropDownListFor中的第一个参数)覆盖了SelectList中的选定值

通过这种方式,可以使用视图模型当前值在模型错误后设置此值

Cyberdrew已经发布了此问题的解决方案,您必须使用SelectList构造函数的第三个参数,它表示默认的选定值:

@Html.DropDownListFor(x => x.InvoiceItem.Count, new SelectList(items, "Value", "Text", 2))

我解决了这个问题。我没有在save button click事件中将dropdownlist发布到控制器。

可能您的问题与我的相同。请检查
x.InvoiceItem.Count
是属性还是字段。如果它是一个字段,post数据将不会绑定到它

我的型号

public class SearchReportObject
{
    public string report_type;
}
@Html.DropDownListFor(model => model.report_type, new SelectList( new List<Object>{new { value = "0" , text = "Red"  },new { value = "1" , text = "Blue" },new { value = "2" , text = "Green"}} , "value", "text"))
在cshtml中

public class SearchReportObject
{
    public string report_type;
}
@Html.DropDownListFor(model => model.report_type, new SelectList( new List<Object>{new { value = "0" , text = "Red"  },new { value = "1" , text = "Blue" },new { value = "2" , text = "Green"}} , "value", "text"))

工作正常。

检查您是否真的将实际表单发送到服务器,或者是否通过AJAX或类似的方式发送自定义JSON数据

我花了一段时间才意识到我的表单并没有直接发送到服务器,“保存”按钮实际上是在发出AJAX请求。因此,修复方法是在JSON数据中的新属性中设置select字段值


现在想想,select元素的值一直被正确绑定。它只是没有按我想象的方式发送。

我想确定我的问题是否清楚,dropdownlist已选择默认值2,但在保存它时,与该下拉列表关联的model属性未设置为2。始终为0。请提供更多建议。我被困在这里了。我想确定我的问题是否清楚,dropdownlist已选择默认值2,但当我保存它时,与该下拉列表关联的model属性未设置为2。它始终为0。我使用了一个标记,但数据没有绑定,即使我指定了它的用途。我将其更改为这个Html帮助程序,并绑定了数据。谢谢