我對 MVC 架構完全陌生,需要幫助將下拉串列中的所選專案保存在資料庫中。基本上對于其創建視圖上的每個專案模型,我需要有一個下拉串列,我需要在其中選擇一個客戶端(客戶端名稱顯示在 Dropdonwlist 中)。根據選擇的客戶端,ClientID 應該通過 Post 方法(作為外鍵)鏈接到資料庫中的 Project 物件實體。
GET 部分來顯示Create和填充Dropdownlist作品,POST 是折磨我的東西。
這是我的專案模型:
public class Project
{
[Key]
public int ProjectId { get; set; }
public string Name { get; set; }
public string Budget { get; set; }
public string BusinessCase { get; set; }
[DataType(DataType.Date)]
public string StartDate { get; set; }
[DataType(DataType.Date)]
public string FinishDate { get; set; }
public int ClientId { get; set; }
public Client Client { get; set; }
public ICollection<ProjectMember> ProjectMembers { get; set; }
public Project()
{
}
}
這是我的客戶端模型:
public class Client
{
[Key]
public int ClientId { get; set; }
public string Name { get; set; }
public string State { get; set; }
public string Address { get; set; }
public int VAT { get; set; }
public ICollection<Project> Projects { get; set; }
public Client()
{
}
}
這是我的 CreateProject ViewModel(可能不需要某些屬性 - 我已經嘗試了很多方法......)
public class CreateProjectViewModel
{
[Key]
public int ProjectId { get; set; }
public string Name { get; set; }
public string Budget { get; set; }
public string BusinessCase { get; set; }
[DataType(DataType.Date)]
public string StartDate { get; set; }
[DataType(DataType.Date)]
public string FinishDate { get; set; }
public int ClientId { get; set; }
public Client Client { get; set; }
public ICollection<ProjectMember> ProjectMembers { get; set; }
public IEnumerable<SelectListItem> Clients { get; set; }
}
這是我用于創建的控制器 GET 方法(使用從客戶端表中選擇值填充下拉串列):
public IActionResult Create()
{
var clients = _context.Client.Select(r => r.Name);
var viewmodel = new CreateProjectViewModel
{
Clients = new SelectList(clients)
};
return View(viewmodel);
}
And Finally, this is is the Create View using the CreateProjectViewModel:
@model ProjectPortfolioApp.ViewModels.CreateProjectViewModel
@{
ViewData["Title"] = "Create";
}
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
@Html.LabelFor(model => model.Client.Name, htmlAttributes: new { @class = "control label col-md-2" })
**@Html.DropDownListFor(model => model.ClientId, Model.Clients, "---Select Client---", new { @class = "form-control" })**
</div>
The above code works fine for displaying the Dropdown elements as expected (Name of the client) using public IEnumerable<SelectListItem> Clients { get; set; }. What I am struggling with is the POST part where I receive error Object reference not set to an instance of an object. When I post model => model.ClientId If I pass here basically anyhing else e.g model => model.Name there is no error only the Client ID is not succesfully posted in database (obviously).
Here is the snippet of HTTP Post which is most probably wrong:
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(CreateProjectViewModel model)
{
if (ModelState.IsValid)
{
var project = new Project() { Name = model.Name, ClientId = model.ClientId, Budget = model.Budget, BusinessCase = model.BusinessCase, StartDate = model.StartDate, FinishDate = model.FinishDate };
_context.Add(project);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View();
}
Can anyone look at my code and help me identify:
- Is my GET Create constructed fine to serve the purpose of POST Create
- What should be passed in @Html.DropDownListFor as valid parameters to avoid the error?
- What should the HTTP Post Create look like?
I looked in dozens of threads, unfortunately nothing helped. Really any help would be appreciated. Thanks.
uj5u.com熱心網友回復:
你必須修復動作
public IActionResult Create()
{
var clients = _context.Client
.Select(r => new SelectListItem{ Value= r.ClientId.ToString(), Text= r.Name)
.ToList();
var viewmodel = new CreateProjectViewModel
{
Clients = clients
.... another properties
};
return View(viewmodel);
}
并查看
@Html.DropDownListFor(model => model.ClientId, @Model.Clients, "---Select Client---", new { @class = "form-control" })
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369484.html
標籤:c# asp.net-mvc model-view-controller viewmodel html.dropdownlistfor
下一篇:如何在工具列左側添加按鈕?
