在MVC 4使用HTML辅助选择下拉 [英] Selected Dropdown using HTML Helpers in mvc 4

查看:102
本文介绍了在MVC 4使用HTML辅助选择下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择HTML辅助下拉。我使用的ViewData请有人帮助我如何与价值选择它传给我的数据的IEnumerable

I need to select the dropdown in html helper. I pass my data in IEnumerable using ViewData please some one help me how to select it with value.

我的code是

@Html.DropDownList("Brand", ViewData["Brand"] as IEnumerable<SelectListItem>, item.BrandMaster, new { @class = "form-control Repeat TableMust"})

下面


  1. 计算机[品牌] 是我的下拉列表。

  2. 我要选择下拉列表中的 item.BrandMaster 值。

  1. ViewData["Brand"] is my Dropdown list.
  2. I want to select item.BrandMaster value in dropdown.

但问题是它显示的文字,但它不包含的值。该值为空的,如何在HTML辅助选择它?

But the problem is it's showing the text but it does not contain the value. The value is "" empty, how to select it in html helpers?

推荐答案

如果你的计算机[品牌] 包含对象的IEnumerable&LT; SelectListItem&GT ; ,我想你已经使用其构造或者更可能的对象初始化,例如:

If your ViewData["Brand"] contains an object IEnumerable<SelectListItem>, I guess you have already used its constructor or more likely Object Initializer, e.g.:

List<SelectListItem> brand = new List<SelectListItem>()
{
   new { Value = 1 , Text = "something 1"  },
   new { Value = 2 , Text = "something 2"  },
   new { Value = 3 , Text = "something 3"  },
};
ViewData["Brand"] = brand;

在您看来,您使用清单作为的DropDownList 帮手第二个参数。您还需要提供什么样的价值和文本。最后一个参数指示值是默认选择:

In your View, you use the list as a second parameter for DropDownList helper. You also need to provide what its Value and Text. The last parameter indicates which value is to be selected by default:

@Html.DopDownList("Brand",
    (List<SelectListItem>)ViewData["Brand"],
    "value", "text", 2)

您也可以尝试的SelectList 容器补的的DropDownList 帮手。只需发送您的对象列表来查看的ViewData等,并把它作为第二个参数。然后提供的值和文本作为第三个和第四个参数,分别为。最后一个参数对应列表中所选项目的值。

You could also try SelectList container to "fill" the DropDownList helper. Simply send your list of Objects to view in ViewData etc., and use it as a second parameter. Then provide Value and Text as the third and the fourth parameters, respectively. The last parameter corresponds to the value of your selected item in the list.

@Html.DropDownList("Brand", new SelectList(
    Model.ListOfYourObjects,
    "<<< Property name as value in the Object, e.g. ID",
    "<<< Property name as text in the Object, e.g. Name/Title/etc.",
    <<< value of thecurrent ID >>>));

例如

public class City
{
    public int ID { get; set; }
    public string Name { get; set; }
}

然后

ViewData["Cities"] = dbContext.Cities.ToList();
//or
ViewData["Cities"] = new List<City>(); // fill this list

在一个视图:

@model List<City>
// ...
@Html.DropDownList(
    "Brand",
    new SelectList(
        Model, "ID", "Name",
        <<< value of ID to be selected or null>>>
    )
);

ID 名称是类属性的名称城市这是为了重要的这里的SelectList 工作

ID and Name are property names in the class City which is important here in order for SelectList to work.

我不能尝试这个code,但是这应该给你一些帮助。希望它可以提供帮助。

I couldn't try this code but this should give you something to work with. Hope it can help.

这篇关于在MVC 4使用HTML辅助选择下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆