我想使用帶有 HttpPost 的 Form 將資料保存在資料庫中,但它沒有被保存。
這是我的控制器 (CustomersController)
添加新客戶的路由(基本上回傳帶有表單的視圖):
[Route("customers/new")]
public ActionResult New()
{
var membershipTypes = _context.MembershipTypes.ToList();
var viewModel = new NewCustomerViewModel
{
MembershipTypes = membershipTypes
};
return View(viewModel);
}
郵寄方式
[HttpPost]
[ValidateAntiForgeryToken]
[Route("customers/create")]
public ActionResult Create(Customer customer)
{
if(!ModelState.IsValid)
{
var viewModel = new NewCustomerViewModel
{
Customer = customer,
MembershipTypes = _context.MembershipTypes.ToList()
};
return View("New", viewModel);
}
if(customer.Id == 0)
_context.Customers.Add(customer);
else
{
var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
customerInDb.Name = customer.Name;
customerInDb.Birthdate = customer.Birthdate;
customerInDb.MemberShipTypeId = customer.MemberShipTypeId;
customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
}
_context.SaveChanges();
return RedirectToAction("Index"); //data is not being saved to db and redirection also does not happen
}
這是視圖的代碼:
@model Vidly.ViewModels.NewCustomerViewModel
@{ViewBag.Title = "New";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>New Customer</h2>
@using(@Html.BeginForm("Create", "Customers"))
{
<div class="form-group">
@Html.LabelFor(m => m.Customer.Name)
@Html.TextBoxFor(m => m.Customer.Name ,new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Customer.Name)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.Birthdate)
@Html.TextBoxFor(m => m.Customer.Birthdate, "{0:d MMM yyyy}", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Customer.Birthdate)
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsletter) Subscribed To Newsletter?
</label>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.MemberShipTypeId)
@Html.DropDownListFor(m => m.Customer.MemberShipTypeId, new SelectList(Model.MembershipTypes, "Id", "Name"), "Select Membership Type" ,new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Customer.MemberShipTypeId)
</div>
@Html.HiddenFor(m => m.Customer.Id);
@Html.AntiForgeryToken()
<button type="submit" class="btn btn-primary">
Submit
</button>
}
我在提交表單和請求時檢查了網路選項卡,并且正在發送資料,但 customer.Id 未設定為任何內容,我認為這是錯誤,但不知道如何解決。這是圖片供參考:

我不知道如何解決這個問題,因為我不明白為什么會這樣。我也有一個編輯操作,它作業正常并更新資料庫中的資料,但創建不起作用。
PS:我知道有一些措辭相似的問題,但它們不能解決我的問題,所以請不要將其標記為重復。
uj5u.com熱心網友回復:
它不起作用,因為您將復雜型別模型從視圖傳遞到控制器。在這種情況下,您必須在控制器中指定如何處理此請求。嘗試檢查您的表單 html 輸出,您會注意到每個屬性,例如:m.Customer.MemberShipTypeId 呈現為 Customer_MemberShipTypeId 等。要解決此問題,請更改您的方法以系結復雜模型的每個前綴。例子:
public ActionResult Create([Bind(Prefix = "Customer")] Customer customer)
{
}
嘗試也改變這個
@using (Html.BeginForm("Create", "Customers", FormMethod.Post, new { @class = "form", role = "form" }))
{
}
uj5u.com熱心網友回復:
您的操作應與表單模型具有相同的輸入引數
public ActionResult Create(NewCustomerViewModel model)
您還可以為 customerId 創建隱藏欄位,但它對創建沒有多大意義,因為它始終為 0
@Html.HiddenFor(m => m.Customer.Id)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359182.html
標籤:C# 网站 asp.net-mvc 实体框架
上一篇:物體框架,LINQ查詢
