asp.net-mvc - 使用 View 模型时更新数据库

标签 asp.net-mvc mvvm viewmodel

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

假设我有这两个类:

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

这些模型确实过于简化了,但我只是想了解一下上下文。可以说我想为假设要向数据库添加条目的 View 创建一个 View 模型。我如何从 View 模型向“广告”数据库表添加信息。假设 View 模型看起来像:

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 类中的参数(尽管我需要 View 模型来呈现下拉列表)。但是我如何从 CreateViewModel 中提取它以添加到数据库中。

这是我目前的代码:

    [HttpPost]
    public ActionResult Create(Ad ad)
    {

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

        return View(ad);

因为这是一个 Ad 类,我如何从 View 模型中只提取 Ad 参数并将其插入到数据库中。 抱歉,帖子很长,可能还有一些严肃的新手内容。我只是不知道如何更好地解释它。

如果有人可以解释 View 模型,或者将我引导到一些这样做的网站,我将不胜感激。

/米

最佳答案

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

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

Controller

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

创建汽车(创建汽车.cshtml)

@model YourSolutionName.ModelsFolder.CreateCarViewModel

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

再次 Controller

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

关于asp.net-mvc - 使用 View 模型时更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10200596/

相关文章:

javascript - RelaodGrid 操作时 jqGrid PostData 中的 DateTime

asp.net-mvc - asp.net(mvc) 服务器应该如何将错误返回给 jquery ajax 调用以在错误回调中捕获?

asp.net-mvc - ASP.NET MVC 项目中有一个部分是用 Angular2 : a build error occurs when using MvcBuildViews 编写的

mvvm - 如何使用 MVVM 清除 WPF 4.0 中的 DatePicker 值

mvvm - 如何根据.net maui 中的条件进行绑定(bind)?

c# - 如何在自定义事件参数中传递 View 模型

javascript - 与 Knockout 相比,Angular 中表示对象的方式

javascript - 使用 .NET MVC 中的 Response.Filter 将所有 JavaScript 保留在页面底部

c# - MVC ViewModels 和 Entity Framework 查询

android - 如何在Activity的另一个方法中使用LiveData值?