Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在ASP.NET MVC中更新表问题_Sql_Asp.net Mvc_Entity Framework_Annotations - Fatal编程技术网

Sql 在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

我有一个ASP.NET MVC模型类,如下所示:

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);
}