【问题标题】:Dropdownlist for not binding to model property mvc3不绑定到模型属性 mvc3 的下拉列表
【发布时间】:2013-07-01 17:13:15
【问题描述】:

MVC3 DropdownListFor 未将模型属性绑定到选定值 这是我的看法

@{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"))

InvoiceModel 有一个称为 InvoiceItem 类的属性,该类进一步具有 int 类型的属性 Count。 count 属性始终为 0,并且不会更新为从下拉列表中选择的值。

请帮助我已经花了几个小时。谢谢。


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

我用过 @Html.DropDownListFor(x => x.InvoiceItem.Count, new SelectList(items, "value", "text", 2))

也试过@Html.DropDownListFor(x => x.InvoiceItem.Count, new SelectList(items, "value", "text", "2"))

Count 属性始终为 0。我在这里缺少什么。

【问题讨论】:

  • 我想确保我的问题很清楚,下拉列表选择了默认值 2,但是当我保存它时,与下拉列表关联的模型属性未设置为 2。它总是0.

标签: asp.net-mvc-3


【解决方案1】:

用途:

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

例如:

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

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

【讨论】:

  • 请有任何进一步的建议。我被困在这里了。
【解决方案2】:

也许你的问题和我的一样。请检查x.InvoiceItem.Count 是属性还是字段。如果是字段,post 数据不会绑定到它。

我的模型

public class SearchReportObject
{
    public string report_type;
}

在cshtml中

@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"))

在帖子表单上,report_type 的值始终为 null。但是当我将report_type 从一个字段更改为这样的属性时:

public class SearchReportObject
{
    public string report_type{ set; get; }
}

效果很好。

【讨论】:

    【解决方案3】:

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

    这样,您的视图模型当前值可用于在模型错误后设置此值。

    Cyber​​drew 已经发布了这个问题的解决方案,你必须使用 SelectList 构造函数的第三个参数,它代表默认选择值:

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

    【讨论】:

    • 我想确保我的问题很清楚,下拉列表选择了默认值 2,但是当我保存它时,与下拉列表关联的模型属性未设置为 2。它总是0.
    • 我使用了
    【解决方案4】:

    我发现了这个问题。我没有在保存按钮单击事件中将下拉列表发布到控制器。

    【讨论】:

      【解决方案5】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 2014-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多