我的ViewModel只返回id asp net core 2.0 tag helper

时间:2017-08-24 18:49:53

标签: c# asp.net asp.net-mvc

我只能使用asp标签助手从我的表单asp.net Core获取dishId。我已尝试过asp-route和提交表单,但没有任何帮助。我不知道我做错了什么。请告诉我是否需要添加一些内容以便您了解我。我通常不会在这里发帖,但我几乎使用论坛。有人能帮我吗?

 public IActionResult Edit(EditDishViewModel model)
   {
    return null;
   }

在控制器的这一行中,模型只返回EditDish并且它是id。没别了。

查看

@model PizzAkuten.Models.EditDishViewModel

@{
    ViewData["Title"] = "Edit";
}

<h2>Edit</h2>

<h4>Dish</h4>
<form asp-action="Edit" asp-controller="Order" method="post">
    <input type="hidden" asp-for="EditDish.DishId" />
    <div class="form-group">
        <label asp-for="EditDish.Name" class="control-label"></label>
        <input asp-for="EditDish.Name" class="form-control" 
               value="@Model.EditDish.Name" disabled/>
</div>
<div class="form-group">
    <label asp-for="EditDish.Price" class="control-label"></label>
    <input asp-for="EditDish.Price" class="form-control" 
      value="@Model.EditDish.Price" disabled/>
   </div>
         <label asp-for="EditDish.DishIngredients" class="control-
     label">Ingredienser</label>
      <div class="form-group">

    @foreach (var dish in Model.EditDish.DishIngredients)
    {
        <label asp-for="@dish.Ingredient.Name" class="control-
 label">@dish.Ingredient.Name</label>
        <input asp-for="@dish.Ingredient.IsChecked" class="form-control" 
 type="checkbox" value="true" />

    }
   </div>
<div class="form-group">

    @foreach (var ing in Model.ExtraIngredients)
    {
        <label asp-for="@ing.Ingredients.Name" class="control-
    label">@ing.Ingredients.Name</label>
        <input asp-for="@ing.IsChecked" class="form-control" type="checkbox" 
  value=""/>
    }
   </div>
     <div class="form-group">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</form>

   <div>
      <a asp-action="Index">Back to List</a>
     </div>

控制器

   public async Task<IActionResult> Edit(int dishId)
    {
        if (dishId == 0)
        {
            return NotFound();
        }
        var ingredients = _context.Ingredients.ToList();
        var dish = await _context.Dishes.Include(d => d.DishIngredients).ThenInclude(di => di.Ingredient)
            .SingleOrDefaultAsync(m => m.DishId == dishId);

        var model = new EditDishViewModel();

        var ingList = new List<ExtraIngredient>();

        foreach (var item in ingredients)
        {
            var ing = new ExtraIngredient();
            item.IsChecked = true;
            ing.Ingredients = item;
            ingList.Add(ing);
        }
        model.EditDish = dish;
        model.ExtraIngredients = ingList;

        if (dish == null)
        {
            return NotFound();
        }

        return View(model);
    }

    [HttpPost]
    public IActionResult Edit(EditDishViewModel model)
    {
        return null;
    }

模型

    public class ExtraIngredient
{
    public Ingredient Ingredients { get; set; }

    public bool IsChecked { get; set; }
}



    public int IngredientId { get; set; }
    public string Name { get; set; }


    public bool IsChecked { get; set; }
    public int Price { get; set; }
    public List<DishIngredient> DishIngredients { get; set; }

      public class DishIngredient
{
    public int DishId { get; set; }
    public Dish Dish { get; set; }
    public int IngredientId { get; set; }
    public Ingredient Ingredient { get; set; }
}

    public class EditDishViewModel
{
    public Dish EditDish { get; set; }

    public List<ExtraIngredient> ExtraIngredients { get; set; }
}

  public class Dish
{
    public int DishId { get; set; }

    public string Name { get; set; }

    public int Price { get; set; }

    public string ImagePath { get; set; }

    public int CategoryId { get; set; }

    public Category Category { get; set; }

    [Display(Name = "Ingredients")]
    public List<DishIngredient> DishIngredients { get; set; }
    [Display(Name = "Orderrader")]
    public List<OrderDish> OrderDishes { get; set; }
}

0 个答案:

没有答案