2012-04-17 78 views
1

我有一個問題可以查看模型並將信息添加到數據庫。在使用查看模型時更新數據庫

比方說,我有這兩個類:

public class Ad { 
public int Id { get; set; } 
public int CategoryId { get; set; } 
public string Headline { get; set; } 
public string Text { get; set; } 
public int Type { get; set; } 

public Category Category { get; set; } 
} 

public class Category { 
public int CategoryId { get; set; } 
public int CategoryName { get; set; } 

public IColletion<Ad> Ads { get; set; } 
} 

Context class: 
public DbSet<Ad> Ads { get; set; } 
public DbSet<Category> Categories { get; set; } 

該機型是真的結束了simpified,但我只是想獲得語境的把握。比方說,我想爲視圖創建一個視圖模型,以便將條目添加到數據庫中。我如何着手從視圖模型向「廣告」數據庫表添加信息。比方說,視圖模型看起來類似:

namespace Website.Models 
{ 
public class CreateViewModel 
{ 
    public Ad Ad { get; set; } 
    public ICollection<Categories> Categories { get; set; } 
    public Dictionary<int, string> AdTypes { get; set; } 

    public CreateViewModel() 
    { 
     // to populate a dropdown on the "Create" page 
     this.Adtypes= new Dictionary<int, string> 
           { 
            {1, "For sale"}, 
            {2, "Want to buy"}, 
            {3, "Want to trade"}, 
            {4, "Have to offer"} 
           }; 
    } 
} 
} 

添加到數據庫時,我真正需要的僅僅就是在廣告類的參數(雖然我需要的視圖模型來呈現下拉菜單)。但是,我如何從CreateViewModel中提取這個以添加到數據庫中。

這是我的代碼的時刻:

[HttpPost] 
    public ActionResult Create(Ad ad) 
    { 

     if (ModelState.IsValid) 
     { 
      db.Ads.Add(ad); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(ad); 

由於這是期待一個廣告類,我如何從視圖模型僅提取廣告PARAMATERS並將其插入到數據庫。 對不起,很長的帖子,可能是一些嚴重的新手東西。我只是不知道如何更好地解釋它。

我將不勝感激,如果有人可以解釋有關視圖模型,或指示我到一些網站。

/平方米

回答

6

,當你需要的網站像下拉菜單值更多的數據可以使用的ViewModels。所以,讓我們說你想創造一輛汽車。

Car對象(Car.cs)

public class Car 
{ 
    public int Id {get;set;} 
    public string Color {get;set;} 
    public string Name {get;set;} 
} 

但你不希望自己在文本框中鍵入顏色。假設你想從下拉菜單中選擇顏色。如果是這樣,你需要添加顏色列表(SelectList)的下拉列表。

視圖模型是在這種情況下有益的(CreateCarViewModel.cs)

public CreateCarViewModel 
{ 
    public Car Car {get;set;} 
    public SelectList Colors{ get; set; } //List of colors for dropdown 
} 

控制器

ActionResult CreateCar() 
{ 
    CreateCarViewModel CCVM = new CreateCarViewModel(); 
    List<string> colors = new List<string>{"Black","White"}; 
    CCVM.Colors = new SelectList(colors); 

    //Your view is expecting CreateCarViewModel object so you have to pass it 
    return View(CCVM); 
} 

CreateCar(CreateCar.cshtml)

@model YourSolutionName.ModelsFolder.CreateCarViewModel 

//form etc. 
{ 
    @Html.DropDownListFor(x => x.Car.Color, Model.Colors) 
    @Html.TextBoxFor(x => x.Car.Name) 
} 

控制器再次

[HttpPost] 
//Again: but now controller expects CreateCarViewModel 
ActionResult CreateCar(CreateCarViewModel CCVM) 
{ 
    if (ModelState.IsValid) 
    //update database with CCVM.Car object and redirect to some action or whatever you want to do 
    else 
    { 
    //populate your colors list again 

    List<string> colors = new List<string>{"Black","White"}; 
    CCVM.Colors = new SelectList(colors); 
    return View (CCVM); 
    } 
} 
+0

Mariusz:非常感謝你的出色解釋。我有點想在這些方向,但我永遠不能得到它的工作。這一定會幫助我! – 2012-04-18 11:36:08

+0

我很高興能幫上忙。 – Mariusz 2012-04-18 11:37:01