我无法在 ASP.Net 的表单中获取 FoodSelectedId

问题描述 投票:0回答:1

这是我以我的形式向厨师添加食物的方法:我想添加一些之前创建的食物


        [HttpGet]
        [Route("CreateNewChefFood")]
        public async Task<IActionResult> CreateNewChefFood(int id)
        {
            if (id <= 0) return NotFound();
            //var chef= await _foodService.GetChefById(id);
            await GetAllFoodList();
            await GetAllFoodPriceType();

     


            return View();
        }


        [HttpPost]
        [Route("CreateNewChefFood")]
        public async Task<IActionResult> CreateNewChefFood(CreateChefFoodViewModel createChefFoodViewModel)
        {
           
            if (ModelState.IsValid)
            {
                await GetAllFoodList();
                await GetAllFoodPriceType();
             
                var result = await _foodService.CreateChefFoodByAdmin(createChefFoodViewModel);

                switch (result)
                {
                    case CreateChefFoodResult.NotFound:
                        return RedirectToAction("NotFound", "Home");



                    case CreateChefFoodResult.Success:
                        ViewBag.SuccessText = "Chef food Was Created Successfully";
                        return RedirectToAction("FilterChefList", "Chef");
                }
            }

            await GetAllFoodList();
            await GetAllFoodPriceType();
        
            return View(createChefFoodViewModel);
        }

我想以表单形式获取 FoodId,但我不能,而且它不接受 FoodSelectedId

这是我的视图模型


    public class CreateChefFoodViewModel
    {
       
        public int FoodSelectedId { get; set; }
        public int ChefId { get; set; }

.
.   

.

        [Display(Name = "PriceType")]
        public List<int> SelectedPriceType { get; set; }
      

    }

这是我观点的一部分,我将我的食物名称作为查看日期发送以查看并正确使用 get 方法获取它

       <form asp-controller="Chef" asp-action="CreateNewChefFood"   method="post" enctype="multipart/form-data">
           <input type="hidden" asp-for="ChefId" />
           <input type="hidden" asp-for="FoodId" />
           <div class="form-row">

               <div class="col-lg-8">

                   <div class="basic-form">

                       <div class="form-group">
                           <label asp-for="FoodSelectedId">Select Food from list:</label>
                           
                           <select asp-for="FoodId" name="FoodId" class="form-control">

                               @if (foodList.Any())
                               {
                                   @foreach (var item in foodList)
                                   {
                                       <option value="@item.FoodId"> @item.FoodTitle </option>
                                   }
                               }

                           </select>
                           <span asp-validation-for="FoodId"></span>
                       </div>

我发送了食物列表作为查看数据

@{
    ViewData["Title"] = "CreateNewChefFood";
    List<FoodViewModel>? foodList = ViewData["FoodList"] as List<FoodViewModel>;
    List<PriceTypeTitleViewModel>? priceType = ViewData["PriceType"] as List<PriceTypeTitleViewModel>;
}
asp.net database asp.net-core post viewdata
1个回答
0
投票

由于您在表单中指定了输入名称

<select asp-for="FoodId" name="FoodId" class="form-control">

模型定义也要使用对应的name属性,否则该字段无法下发。

public class CreateChefFoodViewModel
{
    [Display(Name = "FoodId")]
    public int FoodSelectedId { get; set; }
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.