为视图中的项目创建 DropDownList

发布于 2024-12-23 15:11:40 字数 281 浏览 1 评论 0原文

我想创建一个具有模型数据之一绑定的 DropDownList,但我希望在视图中构建下拉项,并且我不希望这些项出现在模型数据中或来自我的控制器。您能否建议如何在视图中构建选择列表 基本上我想创建这样的东西:

<%= Html.DropDownListFor(model=>model.SomeProperty,<<create the dropdown list items here>> %>

请提出建议。

-桑帕特。

I want to create a DropDownList with a binding for one of my model data, but i want the dropdown items to be built within the View and i do not want the items coming in the Model data or from my controller. Can you please suggest how to build the selectlist within View
Basically I want to create something like this:

<%= Html.DropDownListFor(model=>model.SomeProperty,<<create the dropdown list items here>> %>

Please suggest.

-Sampat.

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

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

发布评论

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

评论(2

一个人的夜不怕黑 2024-12-30 15:11:40

你不能那样使用它。顾名思义,它用于为对象中的每个属性返回一个 HTML 选择元素,该对象由使用指定列表项和 HTML 属性的指定表达式表示。

尽管您可以在视图中创建此列表对象,如下所示:-

@Html.DropDownListFor(model => model.DropDownElement, 
                       new SelectList(model.DropDownElement, "Id", "Name"))

更新

我将以带有 Id/Name 对的国家/地区模型为例。现在

public class Country 
{
    public int Id { get; set; }
    public string Name { get; set; }
}

,在控制器操作中,您可以将其作为选择列表传递:

public ActionResult YourAction()
{
    YourModel yourModel = new YourModel(); // Just for reference. I suppose you must be passing some model to your view
    ViewBag.DropDownList = new SelectList(db.Countries, "Id", "Name"); // This way you don't need to make any changes with your passing model.
    return View(yourModel);
}

最后在视图中,您可以按以下方式使用 DropDownListFor。

@Html.DropDownListFor(model => model.YourModelProperty, 
   (IEnumerable<SelectListItem>)ViewBag.DropDownList, "---Select a value---") 

在 Sidenote 中,如果您只想显示具有值的数字列表,您可以直接输入 HTML 并使用它,而不是使用 DropDownListFor。就像下面一样,

<select id="yourModelPropertyName" name="yourModelPropertyName">
   <option value="">---Select Value---</option>
   <option value="1">India</option>
   <option value="2">Australia</option>
   <option value="3">US</option>
   <option value="4">England</option>
   <option value="5">Finland</option>
</select>

只需确保“yourModelPropertyName”正确,并且它应该是您想要更新的属性的名称

更多更新

在您不想显示所选值的视图中,请使用以下内容代码

<select id="yourModelPropertyName" name="yourModelPropertyName">
   <option selected="selected" value="1">@model.YourDropDownList</option>
   <option value="2">India</option>
   <option value="3">Australia</option>
</select>

这应该可以解决问题:-)

You can't use it that way. As the name suggests its for returning an HTML select element for each property in the object that is represented by the specified expression using the specified list items and HTML attributes.

Though you can create this list object in view like following :-

@Html.DropDownListFor(model => model.DropDownElement, 
                       new SelectList(model.DropDownElement, "Id", "Name"))

Update

I will take the example of a Country Model with an Id/Name pair. Like following

public class Country 
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now, in your controller action, you can pass it as a selectlist:

public ActionResult YourAction()
{
    YourModel yourModel = new YourModel(); // Just for reference. I suppose you must be passing some model to your view
    ViewBag.DropDownList = new SelectList(db.Countries, "Id", "Name"); // This way you don't need to make any changes with your passing model.
    return View(yourModel);
}

And finally in View, you can use the DropDownListFor in the following manner.

@Html.DropDownListFor(model => model.YourModelProperty, 
   (IEnumerable<SelectListItem>)ViewBag.DropDownList, "---Select a value---") 

On a Sidenote, if you just want to display a list of numbers with value, you can directly enter the HTML and utilize it, rather than using the DropDownListFor. Like follwing

<select id="yourModelPropertyName" name="yourModelPropertyName">
   <option value="">---Select Value---</option>
   <option value="1">India</option>
   <option value="2">Australia</option>
   <option value="3">US</option>
   <option value="4">England</option>
   <option value="5">Finland</option>
</select>

Just make sure that "yourModelPropertyName" is correct and it should be the one for the property where you want it updated

More update

In the views where you wan't to show the selected value, use below code

<select id="yourModelPropertyName" name="yourModelPropertyName">
   <option selected="selected" value="1">@model.YourDropDownList</option>
   <option value="2">India</option>
   <option value="3">Australia</option>
</select>

This shall do the trick :-)

丢了幸福的猪 2024-12-30 15:11:40

@Pankaj 给了你一个粗略的方法。您还可以将 SelectListItem 对象 tobject 的 IEnumerable 从控制器传递到您的视图,并基于该对象创建您的选择元素。

这是一个很好的例子:

在 ASP.NET MVC 中使用 Html 选择元素(又名 DropDownList)的方法

想象一下您的控制器看起来像像这样的东西:

public ActionResult Index() {

    var products = productRepo.GetAll();

    registerProductCategorySelectListViewBag();
    return View(products);
}

private void registerProductCategorySelectListViewBag() {

    ViewBag.ProductCategorySelectList = 
        productCategoryRepo.GetAll().Select(
            c => new SelectListItem { 
                Text = c.CategoryName,
                Value = c.CategoryId.ToString()
            }
        );
}

在你看来,DropDownListFor html helper应该看起来像这样:

@Html.DropDownListFor(m => m.CategoryId, 
    (IEnumerable<SelectListItem>)ViewBag.ProductCategorySelectList
)

@Pankaj gave you a rough way of doing it. You can also pass the IEnumerable of SelectListItem object tobject to your view from controller and create your select element based on that.

Here is a good example:

A Way of Working with Html Select Element (AKA DropDownList) In ASP.NET MVC

Imagine that your controller looks like something like this:

public ActionResult Index() {

    var products = productRepo.GetAll();

    registerProductCategorySelectListViewBag();
    return View(products);
}

private void registerProductCategorySelectListViewBag() {

    ViewBag.ProductCategorySelectList = 
        productCategoryRepo.GetAll().Select(
            c => new SelectListItem { 
                Text = c.CategoryName,
                Value = c.CategoryId.ToString()
            }
        );
}

on your view, DropDownListFor html helper should look like something like this:

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