Sql 在ASP.NET MVC中更新表问题
我有一个ASP.NET MVC模型类,如下所示:Sql 在ASP.NET MVC中更新表问题,sql,asp.net-mvc,entity-framework,annotations,Sql,Asp.net Mvc,Entity Framework,Annotations,我有一个ASP.NET MVC模型类,如下所示: using System.Collections.Generic; using System.ComponentModel.DataAnnotations; public partial class Customer { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInCons
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Customer()
{
this.Rates_Comments = new HashSet<Rates_Comments>();
}
[Required]
public double ID { get; set; }
[Required]
public string Marital_Status { get; set; }
[Required]
public string Gender { get; set; }
[Required]
[Range(5000,500000)]
public double Income { get; set; }
[Required]
public double Children { get; set; }
[Required]
public string Education { get; set; }
[Required]
public string Occupation { get; set; }
[Required]
public string Home_Owner { get; set; }
我的问题是,由于[必需]
数据注释,我无法更新表中的一个单元格。例如,用户必须访问其帐户才能编辑它(如果需要),因此他只能编辑一个字段,而不是全部字段
而由于注册问题,所有备案都是必需的,那么如何克服这个问题呢
<h2>Update My Account:</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-inline">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group" style="width:380px;">
<div class="form-group">
@Html.Label("Email-User Name", htmlAttributes: new { @class = "control-label col-md-10" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @style = "width:220px", @readonly = "readonly" } })
<br />
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("First Name", htmlAttributes: new { @class = "control-label col-md-10" })
<div class="col-md-10">
@Html.EditorFor(model => model.F_Name, new { htmlAttributes = new { @class = "form-control", @style = "width:220px" } })
<br />
@Html.ValidationMessageFor(model => model.F_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Last Name", htmlAttributes: new { @class = "control-label col-md-10" })
<div class="col-md-10">
@Html.EditorFor(model => model.L_Name, new { htmlAttributes = new { @class = "form-control", @style = "width:220px" } })
<br />
@Html.ValidationMessageFor(model => model.L_Name, "", new { @class = "text-danger" })
</div>
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Marital_Status,Gender,Income,Children,Education,Occupation,Home_Owner,Cars,Commute_Distance,Region,Age,Buy,OrderNumber")] Customer customer)
{
int[] Children = new int[] { 0, 1, 2, 3, 4, 5 };
ViewBag.OrderNumber = new SelectList(db.Orders, "OrderNumber", "OrderNumber");
ViewBag.Gender = new SelectList(db.Customers.Select(x => x.Gender).Distinct());
ViewBag.email = new SelectList(db.Customers.Select(x => x.Email).Distinct());
ViewBag.fn = new SelectList(db.Customers.Select(x => x.F_Name).Distinct());
ViewBag.ln = new SelectList(db.Customers.Select(x => x.L_Name).Distinct());
ViewBag.pas = new SelectList(db.Customers.Select(x => x.Pass).Distinct());
ViewBag.Marital_Status = new SelectList(db.Customers.Select(x => x.Marital_Status).Distinct());
ViewBag.Education = new SelectList(db.Customers.Select(x => x.Education).Distinct());
ViewBag.Occupation = new SelectList(db.Customers.Select(x => x.Occupation).Distinct());
ViewBag.Home_Owner = new SelectList(db.Customers.Select(x => x.Home_Owner).Distinct());
ViewBag.Cars = new SelectList(db.Customers.Select(x => x.Cars).Distinct());
ViewBag.Commute_Distance = new SelectList(db.Customers.Select(x => x.Commute_Distance).Distinct());
ViewBag.Region = new SelectList(db.Customers.Select(x => x.Region).Distinct());
ViewBag.Children = new SelectList(Children);
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.OrderNumber = new SelectList(db.Orders, "OrderNumber", "OrderNumber", customer.OrderNumber);
return View(customer);
}