ASP.NET 即使发送了请求,MVC 表单数据也没有保存到数据库中
ASP.NET MVC Form data is not being saved into database even though the request is sent
我想使用 Form 和 Http 将数据保存在数据库中Post,但它没有被保存。
这是我的控制器 (CustomersController)
添加新客户的路线(基本上 returns 带有表单的视图):
[Route("customers/new")]
public ActionResult New()
{
var membershipTypes = _context.MembershipTypes.ToList();
var viewModel = new NewCustomerViewModel
{
MembershipTypes = membershipTypes
};
return View(viewModel);
}
Post方法
[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:我知道有几个措辞相似的问题,但它们没有解决我的问题,所以请不要将其标记为重复。
它不起作用,因为您正在将复杂类型模型从视图传递到控制器。在这种情况下,您必须在控制器中指定如何处理此请求。尝试检查您的表单 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" }))
{
}
您的操作应具有与表单模型相同的输入参数
public ActionResult Create(NewCustomerViewModel model)
您还可以为 customerId 创建隐藏字段,但它对创建没有多大意义,因为它始终为 0
@Html.HiddenFor(m => m.Customer.Id)
我想使用 Form 和 Http 将数据保存在数据库中Post,但它没有被保存。
这是我的控制器 (CustomersController)
添加新客户的路线(基本上 returns 带有表单的视图):
[Route("customers/new")]
public ActionResult New()
{
var membershipTypes = _context.MembershipTypes.ToList();
var viewModel = new NewCustomerViewModel
{
MembershipTypes = membershipTypes
};
return View(viewModel);
}
Post方法
[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:我知道有几个措辞相似的问题,但它们没有解决我的问题,所以请不要将其标记为重复。
它不起作用,因为您正在将复杂类型模型从视图传递到控制器。在这种情况下,您必须在控制器中指定如何处理此请求。尝试检查您的表单 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" }))
{
}
您的操作应具有与表单模型相同的输入参数
public ActionResult Create(NewCustomerViewModel model)
您还可以为 customerId 创建隐藏字段,但它对创建没有多大意义,因为它始终为 0
@Html.HiddenFor(m => m.Customer.Id)