我正在用 ASP.net mvc 制作一個 web 應用程序。您可以在以下位置查看食譜:/recipe問題是當我在 /recipe/create 添加食譜時,它不起作用。我希望它顯示在 /recipe 頁面上。食譜頁面所以我認為控制器或視圖有問題,但我認為是控制器。這是配方控制器代碼: public class RecipeController : Controller{ List<RecipeViewModel> vm = new List<RecipeViewModel>(); public IActionResult Index() { foreach (var recept in MockDataRecipeFactory.GetRecipes()) { vm.Add(new RecipeViewModel { Id = recept.Id, Name = recept.Name, Category = recept.Category, NumberOfIngredients = recept.Ingredients.Count }); } return View(vm); } public IActionResult Create() { return View(); } [HttpPost] public IActionResult Create(RecipeViewModel recipemodel) { vm.Add(new RecipeViewModel { Name = recipemodel.Name, Id = recipemodel.Id, Category = recipemodel.Category, NumberOfIngredients = recipemodel.NumberOfIngredients }); return RedirectToAction("Index"); }我所做的是,我在頂部有一個 RecipeViewModel 列表,并將創建的項目添加到該列表中。這是 RecipeViewModel : public class RecipeViewModel{ public int Id { get; set; } [DisplayName("Naam")] public string Name { get; set; } [DisplayName("Categorie")] public RecipeCategory Category { get; set; } [DisplayName("Aantal")] public int NumberOfIngredients { get; set; }}這是視圖中的表單:<div class="row"><div class="col-md-4"> <form asp-action="Create"> <div class="form-group"> <label asp-for="Id">Id:</label> <input asp-for="Id" type="text" class="form-control" name="id" /> </div> <div class="form-group"> <label asp-for="Name">Naam:</label> <input asp-for="Name" type="text" class="form-control" name="name" /> </div>因此,當我添加食譜時,它會返回到食譜頁面,但不會顯示添加的食譜。我做錯了什么?創建食譜頁面
ASP dot net mvc - 將項目添加到列表不起作用,它什么也不做
慕工程0101907
2022-10-15 14:45:40