【问题标题】:Updating database when using View Models使用视图模型时更新数据库
【发布时间】:2012-04-29 08:17:27
【问题描述】:

我有一个关于查看模型和向数据库添加信息的问题。

假设我有这两个类:

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

模型确实过于简单,但我只想了解上下文。假设我想为应该向数据库添加条目的视图创建一个视图模型。如何从视图模型向“广告”数据库表添加信息。假设视图模型看起来像这样:

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

添加到数据库时,我唯一真正需要的是 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

【问题讨论】:

    标签: asp.net-mvc mvvm viewmodel


    【解决方案1】:

    当您需要网站上的更多数据(例如下拉菜单的值)时,您可以使用 Viewmodel。所以假设你想制造一辆汽车。

    汽车对象 (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);
      }
    }
    

    【讨论】:

    • Mariusz:非常感谢您的精彩解释。我有点在这些方向上思考,但我永远无法让它发挥作用。不过,这肯定会帮助我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2017-03-07
    • 2021-12-17
    相关资源
    最近更新 更多