我正忙于一個 ASP.NET Core MVC 應用程式,我正在嘗試填充一個下拉串列。我創建了一個視圖模型,并向我添加了一個方法,該方法StoresController回傳我想在下拉串列中顯示的商店串列。我一直在研究一些在線教程,因為我對 asp 很陌生。
查看型號:
public class StoreListViewModel
{
public List<StoreList> StoreList { get; set; } = new List<StoreList>();
}
public class StoreList
{
public string StoreId { get; set; } = null!;
public string StoreName { get; set; } = null!;
}
StoresController:
public IActionResult LoadStoreList()
{
if (ModelState.IsValid)
{
var storeList = new StoreListViewModel().StoreList.Select
(x => new SelectListItem { Value = x.StoreId, Text = x.StoreName }).ToList();
ViewBag.Stores = storeList;
}
return NotFound();
}
我正在嘗試使用ViewBag呼叫我的LoadStoreList()方法。
<select name="storeList" class="form-control" asp-items="@(new SelectList(ViewBag.Stores, "Value", "Text"))"></select>
當我加載我的頁面時,我收到以下錯誤
值不能為空。(引數“專案”)
我需要下拉串列的頁面是我的CreateUser.cshtml,它系結到我的UserModel并且有一個UsersController. 我為列出商店而創建的方法在 myStoresController中,它系結到 my StoresModel. 所以我不確定這是否是導致問題的原因。
我已經為此奮斗了好幾天,如果有人可以幫助我完成這項作業或向我展示更好的方法,那就太好了。
*編輯
UserIndex ()方法是我的用戶頁面打開時觸發的第一個方法,我是否從那里呼叫LoadStoreList()方法?
用戶控制器
public async Task<IActionResult> UsersIndex()
{
return _context.UsersView != null ?
View(await _context.UsersView.ToListAsync()) :
Problem("Entity set 'ApplicationDbContext.Users' is null.");
}
uj5u.com熱心網友回復:
我正在嘗試使用 ViewBag 來呼叫我的 LoadStoreList() 方法。
ViewBag 不能用于呼叫任何方法。ViewBag您只需要在呈現您的顯示下拉串列頁面的方法中設定值。
根據您的描述,您說您需要下拉串列的頁面是CreateUser.cshtml. 假設您CreateUser.cshtml使用CreateUser操作呈現頁面。
創建用戶.cshtml:
<select name="storeList" class="form-control" asp-items="@(new SelectList(ViewBag.Stores, "Value", "Text"))"></select>
控制器:
public class YourController : Controller
{
private readonly YourDbcontext _context;
public YourController(YourDbcontext context)
{
_context = context;
}
[HttpGet]
public IActionResult CreateUser()
{
var storeList = _context.StoreLists.Select
(x => new SelectListItem { Value = x.StoreId , Text = x.StoreName }).ToList();
ViewBag.Stores = storeList;
return View();
}
}
YourDbcontext 應該是這樣的:
public class YourDbcontext: DbContext
{
public YourDbcontext(DbContextOptions<MvcProjContext> options)
: base(options)
{
}
public DbSet<StoreList> StoreLists{ get; set; }
}
uj5u.com熱心網友回復:
不要使用 viewbag 來存盤串列資料。使您的視圖頁面模型包括串列,例如:
public class UserCreationViewModel{
public int Id{ get; set; }
public string Name { get; set; }
// Any other properties....
public List<StoreList> StoreList { get; set; }
}
在您的控制器 YourController 中:
[HttpGet]
public IActionResult CreateUser()
{
var storeList = new StoreListViewModel().StoreList.Select
(x => new SelectListItem { Value = x.StoreId, Text = x.StoreName }).ToList();
UserCreationViewModel model=new UserCreationViewModel{
StoreList = storeList
};
return View("createUserViewName", model);
}
在創建用戶視圖名稱中:
@Html.DropDownList("StoreId", new SelectList(Model.StoreList, "StoreId", "StoreName"), "Select", new { @class = "form-control" })
或者
<select class="form-control" asp-for="@Model.StoreId" asp-items="@(new SelectList(Model.StoreList, "StoreId", "StoreName"))">
<option value="-1">Select</option>
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522075.html
