0

我想在我的视图中使用下面的属性和下拉框。我怎么能 ?我已经尝试了很多方法,但
请帮助

public IEnumerable<SelectListItem> Topic
{
    get
    {
        return  new[]
        {
            new SelectListItem { Value = "1", Text = "Science", Selected=true },
            new SelectListItem { Value = "2", Text = "History" },
            new SelectListItem { Value = "3", Text = "Physics" },
        };
    }
}
4

2 回答 2

0

您尚未添加视图代码。

基本上你需要@model 像这样在你的视图中添加一个:

@model MySampleApplication.Models.ModelName

然后在您看来,您应该像这样访问:

@Html.DropdownListFor(m=>m.Topic, Model.Topic)

来自 Scott Allen的带有 ASP.NET MVC 的 DropDownListFor

希望能帮助到你


编辑

假设您有这样的模型类:

public class MyModel
{
public IEnumerable<SelectListItem> Topic
{
    get
    {
        return  new[]
        {
            new SelectListItem { Value = "1", Text = "Science", Selected=true },
            new SelectListItem { Value = "2", Text = "History" },
            new SelectListItem { Value = "3", Text = "Physics" },
        };
    }
}
}

试试这样:

public ActionResult Index() 
{
   MyModel model=new MyModel();
   return View(model);
}
于 2013-03-14T08:54:34.303 回答
0

试试这个

模型

public SelectList Topic
            {
                get
                {
                    return new SelectList(
                      new List<SelectListItem> {
                      new SelectListItem { Value = "1", Text = "Science", Selected = true },
                      new SelectListItem { Value = "2", Text = "History" },
                      new SelectListItem { Value = "3", Text = "Physics" }
                    },"Value","Text"
                    );
                }
            }


public int selectedType {get;set;}

看法

@Html.DropDownListFor(model => model.selectedType , Model.Topic)
于 2013-03-14T08:56:11.463 回答