使用 Html.DropDownListFor() 不使用模型

发布于 2024-12-20 00:21:32 字数 269 浏览 2 评论 0原文

我将一个模型传递到我的视图 (.net mvc 3) 中,该模型无法包含我想要用来填充下拉列表的信息。

我从未使用过 Html.DropDownListFor() 但一直只使用 Html.DropDownList()。我想在我的技巧包中添加另一个工具,因此想尝试合并 Html.DropDownListFor()。

是否可以将此助手与 ViewBag 变量中设置的键/值对一起使用?

请告知,任何代码示例将不胜感激(前端(剃刀)和后端(控制器上))。

TIA

I'm passing a Model into my view (.net mvc 3) that can't contain the information that I want to use to populate my drop down list.

I've never used Html.DropDownListFor() but have always just used Html.DropDownList(). I want to add another tool to my bag of tricks though and so want to try and incorporate Html.DropDownListFor().

Is it possible to use this helper with Key/Value pair set in a ViewBag variable?

Please advise, any code samples would be greatly appreciated (both front end (razor) and back end (on controller)).

TIA

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

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

发布评论

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

评论(1

违心° 2024-12-27 00:21:32

是否可以将此助手与 ViewBag 变量中设置的键/值对一起使用?

绝对不是。如果您想使用强类型帮助程序(例如 Html.DropDownListFor),您需要一个视图模型。由于我总是建议使用视图模型,因此这里有一个示例:

public class MyViewModel
{
    public string SelectedId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

正如您在此示例视图模型中所看到的,对于每个下拉列表,您需要 2 个属性:一个用于保存所选值的标量属性和一个 SelectListItem< 的集合/code> 将保存选项,其中每个选项由值和文本组成。

然后控制器负责初始化此视图模型并将其传递给视图:

public ActionResult Index()
{
    var model = new MyViewModel();
    // normally those values will be dynamic. They will be mapped 
    // to the view model
    model.Items = new[]
    {
        new SelectListItem { Value = "1", Text = "item 1" },
        new SelectListItem { Value = "2", Text = "item 2" },
        new SelectListItem { Value = "3", Text = "item 3" },
    };
    return View(model);
}

然后您将拥有此视图模型对应的强类型视图,您将能够在其中使用强类型版本的助手:

@model MyViewModel
@Html.DropDownListFor(x => x.SelectedId, Model.Items)

以下句子从你的问题中值得关注:

我正在将一个模型传递到我的视图(.net mvc 3)中,该模型不能包含
我想用来填充下拉列表的信息。

您不应该将模型传递给视图。您应该传递一个视图模型。视图模型是您为每个视图专门定义的类。它将包含该视图所需的必要信息。因此,单个视图模型可以由多个模型组成,因为这是视图的要求。

Is it possible to use this helper with Key/Value pair set in a ViewBag variable?

Absolutely not. If you want to use strongly typed helpers such as Html.DropDownListFor you need a view model. And since I always recommend using a view model, here's an example:

public class MyViewModel
{
    public string SelectedId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

As you can see in this sample view model, for each dropdown you need 2 properties: a scalar property that will hold the selected value and a collection of SelectListItem that will hold the options where each option consists of a value and text.

Then it's the controller's responsibility to initialize this view model and pass it to the view:

public ActionResult Index()
{
    var model = new MyViewModel();
    // normally those values will be dynamic. They will be mapped 
    // to the view model
    model.Items = new[]
    {
        new SelectListItem { Value = "1", Text = "item 1" },
        new SelectListItem { Value = "2", Text = "item 2" },
        new SelectListItem { Value = "3", Text = "item 3" },
    };
    return View(model);
}

and then you will have a corresponding strongly typed view to this view model in which you will be able to use the strongly typed version of the helpers:

@model MyViewModel
@Html.DropDownListFor(x => x.SelectedId, Model.Items)

The following sentence from your question deserves some attention:

I'm passing a Model into my view (.net mvc 3) that can't contain the
information that I want to use to populate my drop down list.

You should not be passing a model to a view. You should be passing a view model. A view model is a class that you specifically define for each view. It will contain the necessary information that this view will require. So a single view model could be composed from multiple models simply because such are the requirements of your view.

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