LoginSignup
x9jdzhcc72mxib2azgcyo
@x9jdzhcc72mxib2azgcyo

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

mvc viewbag ドロップダウンで表示から登録まで

Q&AClosed

解決したいこと

vs2022 NET.フレームワーク6.0で開発しています

タイトルの件ですが、編集画面でDBのデータをviewbagを使用して表示し、登録処理まで行いたいと考えています。
表示はドロップダウンリストで行いたいです。
スキャホールドで土台作成しています
コントローラーのアクションメソッドにviewbagコード記述しています。ポストにも同様のを入力しているのみです。
[現状]
編集画面で表示したいデータは表示できているが、登録しようとするとエラー「
キー「ClassCide」を持つViewDataアイテムのタイプは「System.String」ですが、タイプは「IEnumerable」である必要があります。」とでる。
私はこの問題に二日間悩んでいます。

発生している問題・エラー

キー「ClassCide」を持つViewDataアイテムのタイプは「System.String」ですが、タイプは「IEnumerable<SelectListItem>」である必要があります。

該当するソースコード

//HTML 一部抜粋
                          <div asp-validation-summary="ModelOnly" class="text-danger"></div>

                                <input type="hidden" asp-for="Id" />
                                <div class="form-group">
                                    <label asp-for="IngredientsCode" class="control-label"></label>
                                    <input asp-for="IngredientsCode" class="form-control" name="text1"/>
                                    <span asp-validation-for="IngredientsCode" class="text-danger"></span>
                                </div>
                                <div class="form-group">
                                    <label asp-for="IngredientsName" class="control-label"></label>
                                    <input asp-for="IngredientsName" class="form-control" />
                                    <span asp-validation-for="IngredientsName" class="text-danger"></span>
                                </div>
                                <div class="form-group">
                                    <label asp-for="ClassCide" class="control-label"></label>
                                    @Html.DropDownListFor(
                                    model => model.ClassCide,         // プロパティ
                                    (IEnumerable<SelectListItem>)ViewBag.PlanmClass ,  // 選択オプション
                                    new { @class = "select2_multiple form-control " , @id = "UserId", @name = "UserId"}  // その他の属性
                                     ) 
                                    <span asp-validation-for="ClassCide" class="text-danger"></span>
                                </div>
                                <div class="form-group">
                                    <label asp-for="ManufacturerCode" class="control-label"></label>
                                    @Html.DropDownListFor(
                                    model => model.ManufacturerCode,         // プロパティ
                                    (IEnumerable<SelectListItem>)ViewBag.PlanmManufacturer,  // 選択オプション
                                    new { @class = "select2_multiple form-control " , @id = "UserId", @name = "UserId"}  // その他の属性
                                     ) 
                                    <span asp-validation-for="ManufacturerCode" class="text-danger"></span>
                                </div>
                                <div class="form-group">
                                    <label asp-for="WholesaleCode" class="control-label"></label>
                                    @Html.DropDownListFor(
                                    model => model.WholesaleCode,         // プロパティ
                                    (IEnumerable<SelectListItem>)ViewBag.PlanmWholesale,  // 選択オプション
                                     new { @class = "select2_multiple form-control "}  // その他の属性
                                     ) 
//MIngredient Model
     [Required(ErrorMessage = "分類コードは必須入力です。")]
        [Display(Name = "分類コード")]
        public string? ClassCide { get; set; }
        [Required(ErrorMessage = "製造メーカーは必須入力です。")]
        [Display(Name = "製造メーカー")]
        public int ManufacturerCode { get; set; }
        [Required(ErrorMessage = "問屋は必須入力です。")]
        [Display(Name = "問屋")]
        public int WholesaleCode { get; set; }

//controller
 public async Task<IActionResult> Edit(int? id)
        {

            //分類コード
            ViewBag. PlanmClass = _context.MCodes.Where(m => m.Type.Contains("分類")).Select(s => new SelectListItem { Text = s.Code.ToString(), Value = s.Type }).ToList();
            //製造メーカ
            ViewBag.PlanmManufacturer = _context.MCodes.Where(m => m.Type.Contains("製造メーカ")).Select(s => new SelectListItem { Text = s.Code.ToString(), Value = s.Type }).ToList();
            //問屋
            ViewBag.PlanmWholesale = _context.MCodes.Where(m => m.Type.Contains("問屋")).Select(s => new SelectListItem { Text = s.Code.ToString(), Value = s.Type }).ToList();
            

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


            var mIngredient = await _context.MIngredients.FindAsync(id);
            if (mIngredient == null)
            {
                return NotFound();
            }
            return View(mIngredient);


        }

        // POST: MIngredients/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,IngredientsCode,IngredientsName,ClassCide,ManufacturerCode,WholesaleCode,Price,TempFlag,DeleteFlag,CreatedId,CreatedAt,UpdatedId,UpdatedAt")] MIngredient mIngredient)
        {

            ViewBag.PlanmClass = _context.MCodes.Where(m => m.Type.Contains("分類")).Select(s => new SelectListItem { Text = s.Code.ToString(), Value = s.Type }).ToList();
            //製造メーカ
            ViewBag.PlanmManufacturer = _context.MCodes.Where(m => m.Type.Contains("製造メーカ")).Select(s => new SelectListItem { Text = s.Code.ToString(), Value = s.Type }).ToList();
            //問屋
            ViewBag.PlanmWholesale = _context.MCodes.Where(m => m.Type.Contains("問屋")).Select(s => new SelectListItem { Text = s.Code.ToString(), Value = s.Type }).ToList();


            if (id != mIngredient.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(mIngredient);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MIngredientExists(mIngredient.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
             

                return RedirectToAction(nameof(Index));
            }
            return View(mIngredient);
        }
0

1Answer

わんくま掲示板 http://bbs.wankuma.com/index.cgi?mode=al2&namber=99724 で、

このサイトにノイズを増 やしただけという結果に終わったのはどう考えるのかな?

と聞かれたのに対して、

なにも考えないです。他を模索するだけです。第三者のことを気にする余裕なんかありません。

と答えた人だよね。

ここでも他人の迷惑を気にしないでノイズを増やしたということかな?

0

Your answer might help someone💌