【问题标题】:How to bind the model value to @html.DropDownlist如何将模型值绑定到@html.DropDownlist
【发布时间】:2013-02-06 14:09:14
【问题描述】:

我在 ASP.Net MVC3 中工作。我需要在@Html.DropDownlist 中绑定我的模型值。我已经用项目列表填充了我的模型。

如何将我的模型与 MVC DropDownList 绑定?

【问题讨论】:

  • 请发布一些代码以及显示您尝试过的内容

标签: html asp.net-mvc-3 html.dropdownlistfor


【解决方案1】:

你可以这样做:

@Html.DropDownListFor(model => model.MyValue
   new SelectList(Model.MyList, "ValuePropertyName", "TextPropertyName"))

基本上,你需要查找SelectList以及如何使用它。

【讨论】:

    【解决方案2】:

    你应该这样做:

    @Html.DropDownListFor(
        x => x.YourItem, 
        new SelectList(Model.YourItem, "Value", "Text")
    )
    

    在执行此操作之前,您需要将模型传递给您的视图return View(model);

    希望对你有帮助

    【讨论】:

    • 我试过喜欢这个@Html.DropDownListFor(x => x.Projects.ToList(), new SelectList(Model.Projects))
    • 是的,我查看模型结果,我的 cshtml 页面模型在运行时不匹配
    • 请发布您的视图代码、控制器操作和适当的模型类。所以,我们可以看到他们出了什么问题
    • 抱歉,在这种情况下价值是什么?
    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多