2

ビューモデルとデータベースへの情報の追加について質問があります。

私がこれらの2つのクラスを持っているとしましょう:

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; }

モデルは本当に単純化されすぎていますが、コンテキストを把握したいだけです。データベースにエントリを追加することを想定したビューのビューモデルを作成したいとします。ビューモデルから「Ads」データベーステーブルに情報を追加するにはどうすればよいですか。ビューモデルが次のようになっているとしましょう。

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"}
                              };
    }
}
}

dbに追加するときに本当に必要なのは、Adクラスのパラメーターだけです(ただし、ドロップダウンをレンダリングするにはビューモデルが必要です)。しかし、これをCreateViewModelから抽出して、データベースに追加するにはどうすればよいですか。

これは現時点での私のコードです:

    [HttpPost]
    public ActionResult Create(Ad ad)
    {

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

        return View(ad);

これはAdクラスを想定しているので、ビューモデルからAdパラメータのみを抽出してデータベースに挿入するにはどうすればよいですか。申し訳ありませんが、非常に長い投稿とおそらくいくつかの深刻な初心者のもの。私はそれをよりよく説明する方法を知りませんでした。

誰かがビューモデルについて説明してくれるか、説明しているサイトに案内していただければ幸いです。

/ m

4

1 に答える 1

6

ドロップダウンの値など、Web サイトでより多くのデータが必要な場合は、Viewmodels を使用できます。たとえば、車を作りたいとしましょう。

車オブジェクト (Car.cs)

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

しかし、テキストボックスに自分で色を入力したくはありません。ドロップダウンから色を選びたいとしましょう。もしそうなら、何らかの方法で色のリスト (SelectList) をドロップダウンに追加する必要があります。

このような状況では Viewmodel が役立ちます (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);
  }
}
于 2012-04-18T10:10:47.577 に答える