Asp.net mvc Net MVC3中的下拉列表

Asp.net mvc Net MVC3中的下拉列表,asp.net-mvc,Asp.net Mvc,我正在尝试在我的MVC web应用程序中创建dropdonw 模型 控制器 using projectname.Models; { public class DropDownController: Controller { public ActionResult Index() //where Index is one of the view { List <SelectListItem> listItem

我正在尝试在我的MVC web应用程序中创建dropdonw

模型

控制器

using projectname.Models;
{
public class DropDownController: Controller
    {
         public ActionResult Index()   //where Index is one of the view
         {
               List <SelectListItem> listItem = new List<SelectListItem>();
               DropDownModel drop = new DropDownModel();
                drop.id = 1;
                drop.value  = "First";

                listItem.Add(new SelectListItem() {Value = drop.Value, Text = drop.id.toString()});

                return view(listitem);
         }

    }
使用projectname.Models;
{
公共类DropDownController:控制器
{
public ActionResult Index()//其中Index是视图之一
{
List listItem=新列表();
DropDownModel drop=新的DropDownModel();
drop.id=1;
drop.value=“第一”;
添加(新SelectListItem(){Value=drop.Value,Text=drop.id.toString()});
返回视图(列表项);
}
}
}

看法

@{
ViewBag.Title=“主页”;
}
@查看包。留言

要了解有关ASP.NET MVC的更多信息,请访问。


但是,该下拉列表没有显示在“我的索引”视图中。

您需要为您的
视图提供
模型
,它是
列表项


返回视图(列表项)

我建议阅读更多关于MVC的内容。您没有在视图上渲染下拉列表,并且您有一个模型,该模型或多或少执行与listitem相同的操作。这可以由一个对象而不是两个对象来处理。也就是说:

控制器

  public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            DropDownModel drop = new DropDownModel();
            drop.id = 1;
            drop.value = "First";

            listItem.Add(new SelectListItem() { Value = drop.id.ToString(), Text = drop.value });


            return View(listItem);
        }

    }
公共类HomeController:控制器
{
公共行动结果索引()
{
List listItem=新列表();
DropDownModel drop=新的DropDownModel();
drop.id=1;
drop.value=“第一”;
添加(新建SelectListItem(){Value=drop.id.ToString(),Text=drop.Value});
返回视图(列表项);
}
}
查看 注意视图顶部的@Model列表。这将定义作为视图的强类型模型。此模型从控制器(listitem)传递到视图

@model List<SelectListItem>

@{
    ViewBag.Title = "title";
}

@Html.DropDownList("name", Model)

<h2>title</h2>
@型号列表
@{
ViewBag.Title=“Title”;
}
@Html.DropDownList(“名称”,型号)
标题

MVC中有几种显示下拉列表的方法。这是我的路

注意:您需要在模型中收集SelectListItem

模型
公共类MyModel
{
public int SelectedId{get;set;}
公共IList AllItems{get;set;}
公共MyModel()
{
AllItems=新列表();
}
}
控制器
公共类HomeController:控制器
{
公共行动结果索引()
{
var模型=新的MyModel();
model.AllItems=新列表
{
新建SelectListItem{Text=“One”,Value=“1”},
新建SelectListItem{Text=“Two”,Value=“2”},
新建SelectListItem{Text=“Three”,Value=“3”}
};
返回视图(模型);
}
[HttpPost]
公共行动结果索引(MyModel)
{
//获取所选的值
int id=model.SelectedId;
返回视图();
}
}
看法
@model DemoMvc.Controllers.MyModel
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post))
{
@DropDownListFor(x=>x.SelectedId,Model.AllItems)
}

嗨,我试过了,但没用。我是否需要对视图本身进行任何更改?你说的是model.。你是说drop而不是listitem吗?@nitinsh99是的,你必须这样做,但请查看PaulBinder的答案。他的例子看起来很有用。谢谢!正是我想要的。另外,视图中不应该是@model DemoMvc.Models.Mymodel吗?他最喜欢的视图是@model DemoMvc.Controllers.Mymodel,因为他在控制器中声明了他的模型(为了节省示例时间),您的模型应该是model DemoMvc.Models。Mymodel@nitinsh99是的,MyModel应该在Models文件夹中。
  public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            DropDownModel drop = new DropDownModel();
            drop.id = 1;
            drop.value = "First";

            listItem.Add(new SelectListItem() { Value = drop.id.ToString(), Text = drop.value });


            return View(listItem);
        }

    }
@model List<SelectListItem>

@{
    ViewBag.Title = "title";
}

@Html.DropDownList("name", Model)

<h2>title</h2>
public class MyModel
{
    public int SelectedId { get; set; }
    public IList<SelectListItem> AllItems { get; set; }

    public MyModel()
    {
        AllItems = new List<SelectListItem>();
    }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyModel();
        model.AllItems = new List<SelectListItem>
        {
            new SelectListItem { Text = "One",  Value = "1"},
            new SelectListItem { Text = "Two",  Value = "2"},
            new SelectListItem { Text = "Three",  Value = "3"}
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        // Get the selected value
        int id = model.SelectedId;
        return View();
    }
}
@model DemoMvc.Controllers.MyModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.DropDownListFor(x => x.SelectedId, Model.AllItems)
    <input type="submit" value="Submit" />
}