簡體   English   中英

在MVC 4中從ViewModel創建下拉列表

[英]Creating a Drop Down list from a ViewModel in MVC 4

我是MVC 4的新手,我正在嘗試創建一個下拉列表,它使用我從sql中提取的數據庫中的多個表。 他們將汽車的模型和顏色都放在不同的表格中。

繼承我的ViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Car_ProjectKW.Models;

namespace Car_ProjectKW.ViewModels

{
public class InventoryViewModel
{
    public List<Color> Colors { get; set; }
    public List<Make> Make { get; set; }
    public List<Model> Model { get; set; }
}
}

這是我的控制器

public ActionResult DealerInventory()
    {
        InventoryViewModel viewModel = new
        InventoryViewModel();
       viewModel.Colors = db.Colors.ToList();
       viewModel.Make = db.Makes.ToList();
       viewModel.Model = db.Models.ToList();


        return View();

最后這是我的觀點...我不確定這是否是正確的方法,因為這是什么給我一個錯誤

@model Car_ProjectKW.ViewModels.InventoryViewModel 
       @{
           ViewBag.Title = "DealerInventory";
       }

<h2> Drop Down</h2>
<div>
@*Html.DropDownList("Make")*@
<select>
    @{foreach (var item in Model.Make){
          <option value="@item.MakeDescription">@item.MakeDescription</option>

    }}
</select>
</div>

我對此很新! 任何幫助都會很棒

首先,如果您的模型中沒有捕獲下拉所選值的機制,我不明白為什么需要下拉列表。 所以我首先將您的模型修改為:

public class InventoryViewModel
{
   public int ColorId { get; set; }
   public int MakeId { get; set; }
   public int ModelId { get; set; }
   public List<Color> Colors { get; set; }
   public List<Make> Make { get; set; }
   public List<Model> Model { get; set; }
}

然后,我將做一個擴展方法,將任何IEnumerable轉換為SelectList:

namespace System
{
   public static class SelectListExtensions
   {
      public static SelectList ToSelectList<T>(this IEnumerable<T> items, string dataValueField, string dataTextField) where T : class
      {
         return new SelectList(items, dataValueField, dataTextField);
      }
   }
}

然后在我看來,我會讓這一行生成一個下拉列表:

@Html.DropDownListFor(m => m.ColorId, Model.Colors.ToSelectList("ColorId", "ColorName"), "-- SELECT --")

假設您的Color類具有ColorId和ColorName屬性...

控制器 -

   var PayList = db.Pay.ToList();
 ViewBag.PayList = new SelectList(PayList, "Id", "Name");

查看 -

@Html.DropDownList("Pay", ViewBag.PayList)

這是在剃須刀中使用HTML擴展綁定Dropdown的正確方法。 您還應該簽出@Html.DropDownListFor

需要有關錯誤的更多詳細信息,還要將循環更改為:

 @foreach (var item in Model.Make){
          <option value="@item.MakeDescription">@item.MakeDescription</option>

    }

Ditch ViewBag並使用強類型模型

 @Html.DropDownListFor(x => x.LeagueId, Model.LeagueSL, "--Select League--", new { id = "ddlLeague"})

public ActionResult Index()
    {
        BaseballViewModel model = new BaseballViewModel();

        using (BaseballEntities context = new BaseballEntities())
        {
            var leagueList = context.League.ToList();

            foreach (var item in leagueList)
            {
                model.LeagueSL.Add(new SelectListItem() { Text = item.LeagueName, Value = item.LeagueId.ToString() });
            }

        }
        return View(model);
    }

這是模型屬性,最好在構造函數中實例化它。

 public List<SelectListItem> LeagueSL { get; set; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM