如何制作一个创建控制器及其ViewModel

时间:2019-03-31 21:40:38

标签: c# asp.net-core entity-framework-core

在“创建”视图中,管理员可以创建一个新菜并从下拉列表中选择其类型(DishTypes来自数据库)。问题是我不知道如何在DishesViewModel视图中设计DishesControllerCreate和下拉列表。

这是 菜肴 模型

public class Dishes
{ 
  [Key]
  [Required]
  public int DishID { get; set; }

  [Required]
  public string Dishname { get; set; }

  public int DishTypeID { get; set; }

  [ForeignKey("DishTypeID")]
  public virtual DishTypes DishTypes { get; set; }
}

这是 DishTypes 型号

public class DishTypes
{
    [Required]
    public int DishTypeID { get; set; }

    [Required]
    public string DishTypeName { get; set; }
}

这是我当前的Dishes视图模型(我使用Automapoper将它们映射到域模型)

public class DishesVM
{

   [Required]
   public string DishName{ get; set; }

   [Required]
   public int DishTypeID{ get; set; }

   [Required]
   public string DishTypeName { get; set; }

}

这是 DishesController

public class DishesController: Controller  
{

   [HttpGet]
   public async Task<IActionResult> Create()
   {
      // it should  pass a list of dishTypes to my view so when admins create a dish they can choose dishtype from a dropdown list

     //I down't want to use ViewBag or ViewData
      return View()
   }
}

这是 创建 视图

@model  DataLayers.Models.ViewModels.DishesVM

<form  asp-controller="Dishes" asp-action="Create">
    <label class="label" asp-for="Dishname"></label>
    <input class="input" type="text" asp-for="Dishname">

    //a drop down list, which enables admins choose dishtypes, is needed here
</form

最后是 DishRepository

public class DishRepository
{
   public async Task<IEnumerable<Dishes>> GetAllDishesAsync()
     {
         return await _RepositoryContext.Set<Dishes>().ToListAsync();
     }
}

随意更改所有内容。

1 个答案:

答案 0 :(得分:1)

尝试以下步骤:

  1. 更改视图模型

    public class DishesVM
    {
    
        [Required]
        public string DishName { get; set; }
    
        [Required]
        public int DishTypeID { get; set; }
    
        public List<DishTypes> DishTypes { get; set; }
    }
    
  2. 查看

    <div class="row">
        <div class="col-md-4">
            <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="DishName" class="control-label"></label>
                    <input asp-for="DishName" class="form-control" />
                    <span asp-validation-for="DishName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DishTypeID" class="control-label"></label>
                    <select asp-for="DishTypeID"
                            class="dropdown"
                            asp-items="@(new SelectList(Model.DishTypes, "DishTypeID" ,"DishTypeName"))"></select>
                    <span asp-validation-for="DishTypeID" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
  3. 控制器

    [HttpGet]
    public async Task<IActionResult> Create()
    {
        var types = await _context.DishTypes.ToListAsync();
        var vm = new DishesVM {
            DishTypes = types
        };
        return View(vm);
    }
    [HttpPost]
    public async Task<IActionResult> Create(DishesVM vm)
    {
        return Ok(vm);
    }