簡體   English   中英

我怎樣才能使 controller 中的 selectList 僅返回一個要顯示的選項?

[英]how can i make it so the selectList in the controller returns only one choice to be displayed?

所以我有一個游戲評論頁面形式的項目。 我有 3 個類,即 Review、Maker 和 Game。 后兩者工作正常,包括視圖和控制器。 因為我已經檢查過它們並確認它們確實如此。

還值得一提的是,我使用 Asp.Core Identity 進行身份驗證,登錄和注冊已經沒有錯誤。

現在,至於我想補充什么。 我只是想在游戲卡片下添加一個鏈接,上面寫着“添加評論”。 它將用戶重定向到另一個頁面,該頁面包含一個表單,其中包含用戶的 email、游戲名稱、評論文本以及最后的 5 分分數。

這些圖片展示了我所做的。

主頁在此處輸入圖像描述

表格

在此處輸入圖像描述

為此。 我使用以下選項創建了一個標准的 Controller

  • Controller > 添加 > Controller > MVC Controller 帶視圖

  • 選擇 Review Model 和 db Context。

后一個操作創建了視圖和 controller,其中包含一個具有以下代碼的創建操作:

 public IActionResult Create()
{
    ViewData["GameId"] = new SelectList(_context.Games, "Id", "Name",5);
    ViewData["UserId"] = new SelectList(_context.Users, "Id", "UserName");
    return View();
}

// POST: Reviews/Create
// 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> Create([Bind("id,UserId,GameId,rev,score")] Review review)
{
   if (ModelState.IsValid)
   {                
      _context.Add(review);
      await _context.SaveChangesAsync(); 
      return RedirectToAction(nameof(Index));
   }
   ViewData["GameId"] = new SelectList(_context.Games, "Id", "Name", review.GameId);
   ViewData["UserId"] = new SelectList(_context.Users, "Id", "UserName", review.UserId);
   return View(review);
}

以及包含以下代碼的兩個選擇的創建頁面:

<div class="form-group">
   <label asp-for="UserId" class="control-label"></label>
   <select asp-for="UserId" class ="form-control" asp-items="ViewBag.UserId" ></select>
</div>
<div class="form-group">
   <label asp-for="GameId" class="control-label"></label>
   <select asp-for="GameId" class ="form-control" asp-items="ViewBag.GameId"></select>
</div>

現在我想在用戶想要添加評論時這樣做。 表格只會讓他進入選定的游戲。

我嘗試將代碼更改為以下代碼,希望自動鎖定第 5 個選項,然后直接提交以測試其功能。

ViewData["GameId"] = new SelectList(_context.Games, "Id", "Name",5);
ViewData["UserId"] = new SelectList(_context.Users, "Id", "UserName");

之后的表單頁面

在此處輸入圖像描述

可悲的是,我最終遇到了這個錯誤,我不知道下一步該怎么做錯誤頁面

在此處輸入圖像描述

根據此鏈接中顯示的錯誤,問題出在數據庫的設計上。 對於表Review的ID字段,identity選項是active的,必須由數據庫自動設置。 但是你在 Review controller 中的 Create 方法中設置了 ID 字段,在這種情況下數據庫引擎會遇到這樣的錯誤:Cannot insert explicit value for identity column in table 'Review' when IDENTITY_INSERT is set to OFF。

為了解決這個問題,Insert to database時不要輸入ID值。

  [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("UserId,GameId,rev,score")] Review review)
    {
        if (ModelState.IsValid)
        {                
                _context.Add(review);
                await _context.SaveChangesAsync(); 
            return RedirectToAction(nameof(Index));
        }
        ViewData["GameId"] = new SelectList(_context.Games, "Id", "Name", review.GameId);
        ViewData["UserId"] = new SelectList(_context.Users, "Id", "UserName", review.UserId);
        return View(review);
    }

或者在 Review table for ID 字段中將 Identity 選項更改為 off

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM