DropDown in .Net MVC3

前端 未结 3 2015
盖世英雄少女心
盖世英雄少女心 2021-01-28 03:08

I am trying to create a dropdonw in my MVC web application.

Model

namespace projectname.Models 
{
public class DropDownModel
    {
         public int i         


        
3条回答
  •  [愿得一人]
    2021-01-28 04:10

    I would suggest reading more about MVC. You have nothing rendering the dropdown on your view and you have a model that more or less does the same-thing your listitem is doing. This could be handled by one object instead of two. That said :

    Controller

      public class HomeController : Controller
        {
            public ActionResult Index()
            {
                List listItem = new List();
                DropDownModel drop = new DropDownModel();
                drop.id = 1;
                drop.value = "First";
    
                listItem.Add(new SelectListItem() { Value = drop.id.ToString(), Text = drop.value });
    
    
                return View(listItem);
            }
    
        }
    

    View Note the @Model List at the top of the view. This defines the strongly typed model asigned to the view. This Model is passed from the controller (listitem) to the view.

    @model List
    
    @{
        ViewBag.Title = "title";
    }
    
    @Html.DropDownList("name", Model)
    
    

    title

提交回复
热议问题