2015-07-03 79 views
0

我對MVC相當陌生,我試圖擺脫使用WebForms的形式,但有些事情讓我感到困惑,因爲我需要完成某些工作。MVC綁定模型到視圖中的下拉列表

在我看來,「/areas/admin/cars/form.cshtml」我想用@ Html.Dropdownfor,傳遞的這是由CarManufacturer.cs模型中定義我的汽車製造商的列表。我該怎麼做?

我使用NHibernate的 - 不知道如果讓任何不同...

模型;

public class CarManufacturer 
    { 
     public virtual int Id { get; set; } 
     public virtual string Manufacturer { get; set; } 
     //***do i need the below line?*** 
     public SelectList CarManufacturerList { get; set; } 
    } 

    public class CarManufacturerMap : ClassMapping<CarManufacturer> 
    { 
     public CarManufacturerMap() 
     { 
      Table("CarManufacturers"); 
      Id(x => x.Id, x => x.Generator(Generators.Identity)); 
      Property(x => x.Manufacturer, x => x.NotNullable(true)); 
     } 
    } 

的控制器調用形式是carController.cs

public ActionResult New() 
      { 
       //re-susing views for add and edit 
       return View("Form", new CarsForm 
       { 
        IsNew = true, 
       }); 
      } 

      public ActionResult Edit(int id) 
      { 
       var car = Database.Session.Load<Car>(id); 
       if (car == null) 
        return HttpNotFound(); 

       return View("Form", new CarsForm 
       { 
        ...do stuff here to populate form fields!... 
       }); 
      } 

視圖模型;

public class CarsForm 
    { 
     //do I need the below line? 
     public IList<CarManufacturer> Manufacturers { get; set; } 

     public bool IsNew { get; set; }//whether add or edit action to be used 
     public int? CarId { get; set; } 
...more fields 
} 

非常感謝耐心和幫助新的MVC'er!

回答

0

我通常會將dropdownlist數據添加到viewbag。您可以從視圖模型和實體類中刪除列表。

你的控制器看起來像這樣。

public ActionResult New() 
{ 
    ViewBag.Manufacturers = new SelectList(
     Database.Session.Load<Manufacturer>(), 
     "Manufacturer", 
     "Manufacturer"); 
    return View("Form", new CarsForm 
    { 
     IsNew = true, 
    }); 
} 

public ActionResult Edit(int id) 
{ 
    var car = Database.Session.Load<Car>(id); 
    if (car == null) 
     return HttpNotFound(); 

    ViewBag.Manufacturers = new SelectList(
     Database.Session.Load<Manufacturer>(), 
     "Manufacturer", 
     "Manufacturer", 
     car.Manufacturer); //sets the initial value 
    return View("Form", new CarsForm 
    { 
     ...do stuff here to populate form fields!... 
    }); 
} 

然後在你看來,你只要將你的@Html.DropDownListFor

@Html.DropDownListFor(model => model.Manufacturer, (SelectList)ViewBag.Manufacturers) 
+0

啊,現在我看看viewbag方法是如何工作的,我認爲我更喜歡將模型傳遞到DDL,這是我遇到的問題! :) 謝謝! –

-1

這是一種方法,並且我個人更喜歡小型項目的方法,但有幾種方法可以對這隻特定的貓進行皮膚處理。

希望你會注意到,Html.DropDownListFor需要List<SelectListItem>這樣你就可以繼續前進,並宣佈一個在您的視圖模型,但你也需要特定製造商的Id選擇:

public class CarsForm 
{ 
    public int ManufacturerId { get; set; } 
    public List<SelectListItem> Manufacturers { get; set; } 

    // Other properties 
} 

作爲請注意,視圖模型的命名約定爲CreateCarViewModel

在你New的方法,你需要從你的數據庫實例Manufacturers,通常它會是這樣的:現在

viewModel.Manufacturers = (from m in dbContext.Manufacturers 
          orderby m.Name 
          select new SelectLisItem { Text = m.Name, Value = m.Id.ToString() }).ToList() 

,在你View,你可以這樣做:

@Html.DropDownListFor(model => model.Manufacturers, Model.ManufacturerId, "Please select") 
+0

你可能會得到使用m.Id.ToString()一個錯誤,如果你直接從的DbContext – JamieD77

+0

選擇@ user1221684我'不知道是這種情況,但很容易通過'ToList()'修復,然而 – ediblecode

+0

你也可以使用'SqlFunctions.StringConvert((double)m.Id)' – JamieD77