C# ASP.NET MVC中的复选框列表

C# ASP.NET MVC中的复选框列表,c#,asp.net-mvc-5,checkboxlist,C#,Asp.net Mvc 5,Checkboxlist,我正在为运营维护服务公司开发小票系统 客户打开一张车票并给出一般信息,如位置, 类别说明等 技师解决问题后,他必须提供故障的详细信息 来自预定义集的问题,稍后用于统计报告 我的问题是修复方法无法将更新的值发布到数据库,并且没有添加到缺陷列表表中 注意:我使用教程作为指导 型号: public partial class Tickets { public Tickets() { this.DefectsList = new HashSet<Defects

我正在为运营维护服务公司开发小票系统

  • 客户打开一张车票并给出一般信息,如位置, 类别说明等
  • 技师解决问题后,他必须提供故障的详细信息 来自预定义集的问题,稍后用于统计报告
我的问题是修复方法无法将更新的值发布到数据库,并且没有添加到缺陷列表表中

注意:我使用教程作为指导

型号:

public partial class Tickets
{ 
    public Tickets()
    {
        this.DefectsList = new HashSet<Defects_List>();
    }
    [Key]
    [Display(Name = "Ticket Id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Ticket_Id { get; set; }

    [Display(Name = "Project")]
    public int Project_Id { get; set; }

    [Display(Name = "Issue Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Issue_Date { get; set; }

    [Display(Name = "Category")]
    [DisplayFormat(NullDisplayText = "[Not set]")]
    public int Category_Id { get; set; }

//Other Properties Removed for clarity

    public virtual Business_Category businessCategories { get; set; }
    public virtual ICollection<Defects_List> DefectsList { get; set; }
}
public partial class Business_Category
{
    public Business_Categories()
    {
        this.Tickets = new HashSet<Tickets>();
        this.Malfunctions = new HashSet<Malfunctions>();
    }

    [Key]
    [Display(Name="Category Id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Category_Id { get; set; }

    [Display(Name = "Name (eng)")]
    [StringLength(60, ErrorMessage = "Name cannot be longer than 60 characters.")]
    public string Category_Name { get; set; }

    public virtual ICollection<Tickets> Tickets { get; set; }
    public virtual ICollection<Manufacturers> Manufacturers { get; set; }
    public virtual ICollection<Malfunctions> Malfunctions { get; set; }
}

public partial class Defects_List
{
    [Key, Column(Order = 0)]
    [Display(Name = "Ticket Id")]
    public int Ticket_Id { get; set; }

    [Key, Column(Order = 1)]
    [Display(Name = "Malfunction Id")]
    public int Malfunction_Id { get; set; }

    [StringLength(125, ErrorMessage = "Other cannot be longer than 125  characters.")]
    public string Other { get; set; }

    public virtual ICollection<Tickets> Tickets { get; set; }
    public virtual Malfunctions Malfunctions { get; set; }
}
// GET: /Tickets/Edit/5
    public ActionResult Repair(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var tickets = db.nt_Tickets
                   .Include(t => t.DefectsList).Where(t => t.Ticket_Id == id)
                   .Single();

        if (tickets == null)
        {
            return HttpNotFound();
        }

        PopulateSelectedDefects(tickets);

        //Other codes Removed for clarity

        return View(tickets);
    }

    private void PopulateSelectedDefects(nt_Tickets Tickets)
    {
        int categoryId;

        if (Tickets.Category_Id == 2)
            categoryId = 1;
        else categoryId = Tickets.Category_Id;

        var allDefects = (from m in db.sys_Malfunctions
                        where m.Category_Id == categoryId
                        select m).ToList();

       var ticketDefects = new HashSet<int>(Tickets.DefectsList.Select(t => t.Malfunction_Id));
        var viewModel = new List<TicketDefectsViewModel>();

        foreach (var defect in allDefects)
        {
            viewModel.Add(new TicketDefectsViewModel
            {
                Malfunction_Id = defect.Malfunction_Id,
                Malfunction_Name_e = defect.Malfunction_Name_e,
                IsSelected = ticketDefects.Contains(defect.Malfunction_Id)
            });
        }

        ViewBag.Defects = viewModel;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Repair(int? id, string[] selectedDefects)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var ticketToUpdate = db.nt_Tickets
           .Include(i => i.DefectsList)
           .Where(i => i.Ticket_Id == id)
           .Single();

        if (TryUpdateModel(ticketToUpdate, "",
new string[] {   Ticket_Id,Project_Id,Category_Id,Subject,Workshop_Id,Requested_By,Requestor_Mobile,  Requestor_eMail,Location_Id,Ticket_Status,Periority_Id,Assigned_To,Description,Issue_Date,Created_By,Created_Date,Updated_By,Updated_Date" }))
{
            ticketToUpdate.Updated_By = User.Identity.Name;
            ticketToUpdate.Updated_Date = DateTime.UtcNow;
            db.Entry(ticketToUpdate).State = EntityState.Modified;
            SetTicketDefects(selectedDefects, ticketToUpdate);
            try
            {
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);

                        if (!string.IsNullOrEmpty(message))
                            ViewBag.errorMessage = message;
                    }

                    return View("Error");
                }
                throw raise;
            }
        }

        PopulateSelectedDefects(ticketToUpdate);

        return View("Index");
    }


    private void SetTicketDefects(string[] selectedDefects, Tickets ticketToUpdate)
    {
        if (selectedDefects == null)
        {
            ticketToUpdate.DefectsList = new List<Defects_List>();
            return;
        }

        var selectedDefectsHS = new HashSet<string>(selectedDefects);

        var tcketDefects = new HashSet<int>
            (ticketToUpdate.DefectsList.Select(c => c.Ticket_Id));

        foreach (var defect in db.DefectsLists)
        {
            if (selectedDefectsHS.Contains(defect.Malfunction_Id.ToString()))
            {
                if (!tcketDefects.Contains(defect.Malfunction_Id))
                {
                    ticketToUpdate.DefectsList.Add(defect);
                }
            }
            else
            {
                if (tcketDefects.Contains(defect.Malfunction_Id))
                {
                    ticketToUpdate.DefectsList.Remove(defect);
                }
            }
        }
    }
@using (Html.BeginForm("Repair", "Tickets", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
@*@using (Html.BeginForm())*@
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Ticket_Id)
    @Html.HiddenFor(model => model.Issue_Date)
    @Html.HiddenFor(model => model.Created_By)
    @Html.HiddenFor(model => model.Project_Id)
    @Html.HiddenFor(model => model.Created_Date)        

    <div class="form-group">
        @Html.Label("Ticket_Id", new { @class = "control-label col-md-2" })
        <div class="col-md-1 " style="margin-top:7px">
            @Html.DisplayFor(model => model.Ticket_Id)
        </div>

        @Html.LabelFor(model => model.Issue_Date, new { @class = "control-label col-md-2" })
        <div class="col-md-2" style="margin-top:7px">
            @Html.DisplayFor(model => model.Issue_Date, new { @class = "form-control" })
        </div>

        @Html.LabelFor(model => model.Category_Id, new { @class = "control-label col-md-2" })
        <div class="col-md-3" style="margin-top:7px">
            @Html.DropDownList("Category_Id", null, new { @class = "form-control", @disabled = "disabled" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Subject, new { @class = "control-label col-md-2" })
        <div class="col-md-10" style="margin-top:7px">
            @Html.DisplayFor(model => model.Subject, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
        <div class="col-md-10" style="margin-top:7px">
            @Html.DisplayFor(model => model.Description, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Requested_By, new { @class = "control-label col-md-2" })
        <div class="col-md-5" style="margin-top:7px">
            @Html.DisplayFor(model => model.Requested_By, new { @class = "form-control" })
        </div>

        @Html.LabelFor(model => model.Requestor_Mobile, new { @class = "control-label col-md-2" })
        <div class="col-md-3" style="margin-top:7px">
            @Html.DisplayFor(model => model.Requestor_Mobile, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Requestor_eMail, new { @class = "control-label col-md-2" })
        <div class="col-md-10" style="margin-top:7px">
            @Html.DisplayFor(model => model.Requestor_eMail, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Ticket_Status, new { @class = "control-label col-md-2" })
        <div class="col-md-4" style="margin-top:7px">
            @Html.EnumDropDownListFor(model => model.Ticket_Status, new { @class = "form-control" })
        </div>

        @Html.LabelFor(model => model.Periority_Id, new { @class = "control-label col-md-2" })
        <div class="col-md-4" style="margin-top:7px">
            @Html.EnumDropDownListFor(model => model.Periority_Id, new { @class = "form-control", @disabled = "disabled" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Location_Id, new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("Location_Id", null, new { @class = "form-control", @disabled = "disabled" })
            @Html.ValidationMessageFor(model => model.Location_Id)
        </div>

        @Html.LabelFor(model => model.Assigned_To, new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("Assigned_To", null, new { @class = "form-control", @disabled = "disabled" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-10">
            @Html.Label("Defects")
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <table class="table table-hover">
                <tr>
                    @{
                        int cnt = 0;
                        List<MaintCare.ViewModels.TicketDefectsViewModel> defects = ViewBag.Defects;
                        foreach (var defect in defects)
                        {
                            if (cnt++ % 3 == 0)
                            {
                                @:</tr><tr>
                            }
                            @:<td>
                <input type="checkbox"
                       name="selectedDefects"
                       value="@defect.Malfunction_Id"
                       @(Html.Raw(defect.IsSelected ? "checked=\"checked\"" : "")) />
                                @defect.Malfunction_Name_e

                                @:</td>
                        }
                        @:</tr>
                    }
                </table>
            </div>
        </div>

        <br />
        <div class="form-group">
            <div class="col-md-2">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
   @Html.ActionLink("Back to List", "Index")
</div>
公共部分舱票
{ 
公众票
{
this.DefectsList=new HashSet();
}
[关键]
[显示(Name=“票证Id”)]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共整数票证{get;set;}
[显示(Name=“项目”)]
公共int项目_Id{get;set;}
[显示(Name=“发布日期”)]
[数据类型(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:dd/MM/yyyy}”)]
公共日期时间问题\u日期{get;set;}
[显示(Name=“Category”)]
[显示格式(NullDisplayText=“[未设置]”)]
公共int类_Id{get;set;}
//为清晰起见,删除了其他属性
公共虚拟业务_categorybusinesscategories{get;set;}
公共虚拟ICollection缺陷列表{get;set;}
}
公共部分类商业类
{
公共事业(类别)
{
this.Tickets=newhashset();
this.drughts=newhashset();
}
[关键]
[显示(名称=“类别Id”)]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int类_Id{get;set;}
[显示(Name=“Name(eng)”)]
[StringLength(60,ErrorMessage=“名称不能超过60个字符。”)]
公共字符串类别\u名称{get;set;}
公共虚拟ICollection票证{get;set;}
公共虚拟ICollection制造商{get;set;}
公共虚拟ICollection{get;set;}
}
公共部分类缺陷列表
{
[键,列(顺序=0)]
[显示(Name=“票证Id”)]
公共整数票证{get;set;}
[键,列(顺序=1)]
[显示(名称=“故障Id”)]
public int\u Id{get;set;}
[StringLength(125,ErrorMessage=“其他不能超过125个字符。”)]
公共字符串其他{get;set;}
公共虚拟ICollection票证{get;set;}
公共虚拟机{get;set;}
}
控制器:

public partial class Tickets
{ 
    public Tickets()
    {
        this.DefectsList = new HashSet<Defects_List>();
    }
    [Key]
    [Display(Name = "Ticket Id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Ticket_Id { get; set; }

    [Display(Name = "Project")]
    public int Project_Id { get; set; }

    [Display(Name = "Issue Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Issue_Date { get; set; }

    [Display(Name = "Category")]
    [DisplayFormat(NullDisplayText = "[Not set]")]
    public int Category_Id { get; set; }

//Other Properties Removed for clarity

    public virtual Business_Category businessCategories { get; set; }
    public virtual ICollection<Defects_List> DefectsList { get; set; }
}
public partial class Business_Category
{
    public Business_Categories()
    {
        this.Tickets = new HashSet<Tickets>();
        this.Malfunctions = new HashSet<Malfunctions>();
    }

    [Key]
    [Display(Name="Category Id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Category_Id { get; set; }

    [Display(Name = "Name (eng)")]
    [StringLength(60, ErrorMessage = "Name cannot be longer than 60 characters.")]
    public string Category_Name { get; set; }

    public virtual ICollection<Tickets> Tickets { get; set; }
    public virtual ICollection<Manufacturers> Manufacturers { get; set; }
    public virtual ICollection<Malfunctions> Malfunctions { get; set; }
}

public partial class Defects_List
{
    [Key, Column(Order = 0)]
    [Display(Name = "Ticket Id")]
    public int Ticket_Id { get; set; }

    [Key, Column(Order = 1)]
    [Display(Name = "Malfunction Id")]
    public int Malfunction_Id { get; set; }

    [StringLength(125, ErrorMessage = "Other cannot be longer than 125  characters.")]
    public string Other { get; set; }

    public virtual ICollection<Tickets> Tickets { get; set; }
    public virtual Malfunctions Malfunctions { get; set; }
}
// GET: /Tickets/Edit/5
    public ActionResult Repair(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var tickets = db.nt_Tickets
                   .Include(t => t.DefectsList).Where(t => t.Ticket_Id == id)
                   .Single();

        if (tickets == null)
        {
            return HttpNotFound();
        }

        PopulateSelectedDefects(tickets);

        //Other codes Removed for clarity

        return View(tickets);
    }

    private void PopulateSelectedDefects(nt_Tickets Tickets)
    {
        int categoryId;

        if (Tickets.Category_Id == 2)
            categoryId = 1;
        else categoryId = Tickets.Category_Id;

        var allDefects = (from m in db.sys_Malfunctions
                        where m.Category_Id == categoryId
                        select m).ToList();

       var ticketDefects = new HashSet<int>(Tickets.DefectsList.Select(t => t.Malfunction_Id));
        var viewModel = new List<TicketDefectsViewModel>();

        foreach (var defect in allDefects)
        {
            viewModel.Add(new TicketDefectsViewModel
            {
                Malfunction_Id = defect.Malfunction_Id,
                Malfunction_Name_e = defect.Malfunction_Name_e,
                IsSelected = ticketDefects.Contains(defect.Malfunction_Id)
            });
        }

        ViewBag.Defects = viewModel;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Repair(int? id, string[] selectedDefects)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var ticketToUpdate = db.nt_Tickets
           .Include(i => i.DefectsList)
           .Where(i => i.Ticket_Id == id)
           .Single();

        if (TryUpdateModel(ticketToUpdate, "",
new string[] {   Ticket_Id,Project_Id,Category_Id,Subject,Workshop_Id,Requested_By,Requestor_Mobile,  Requestor_eMail,Location_Id,Ticket_Status,Periority_Id,Assigned_To,Description,Issue_Date,Created_By,Created_Date,Updated_By,Updated_Date" }))
{
            ticketToUpdate.Updated_By = User.Identity.Name;
            ticketToUpdate.Updated_Date = DateTime.UtcNow;
            db.Entry(ticketToUpdate).State = EntityState.Modified;
            SetTicketDefects(selectedDefects, ticketToUpdate);
            try
            {
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);

                        if (!string.IsNullOrEmpty(message))
                            ViewBag.errorMessage = message;
                    }

                    return View("Error");
                }
                throw raise;
            }
        }

        PopulateSelectedDefects(ticketToUpdate);

        return View("Index");
    }


    private void SetTicketDefects(string[] selectedDefects, Tickets ticketToUpdate)
    {
        if (selectedDefects == null)
        {
            ticketToUpdate.DefectsList = new List<Defects_List>();
            return;
        }

        var selectedDefectsHS = new HashSet<string>(selectedDefects);

        var tcketDefects = new HashSet<int>
            (ticketToUpdate.DefectsList.Select(c => c.Ticket_Id));

        foreach (var defect in db.DefectsLists)
        {
            if (selectedDefectsHS.Contains(defect.Malfunction_Id.ToString()))
            {
                if (!tcketDefects.Contains(defect.Malfunction_Id))
                {
                    ticketToUpdate.DefectsList.Add(defect);
                }
            }
            else
            {
                if (tcketDefects.Contains(defect.Malfunction_Id))
                {
                    ticketToUpdate.DefectsList.Remove(defect);
                }
            }
        }
    }
@using (Html.BeginForm("Repair", "Tickets", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
@*@using (Html.BeginForm())*@
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.Ticket_Id)
    @Html.HiddenFor(model => model.Issue_Date)
    @Html.HiddenFor(model => model.Created_By)
    @Html.HiddenFor(model => model.Project_Id)
    @Html.HiddenFor(model => model.Created_Date)        

    <div class="form-group">
        @Html.Label("Ticket_Id", new { @class = "control-label col-md-2" })
        <div class="col-md-1 " style="margin-top:7px">
            @Html.DisplayFor(model => model.Ticket_Id)
        </div>

        @Html.LabelFor(model => model.Issue_Date, new { @class = "control-label col-md-2" })
        <div class="col-md-2" style="margin-top:7px">
            @Html.DisplayFor(model => model.Issue_Date, new { @class = "form-control" })
        </div>

        @Html.LabelFor(model => model.Category_Id, new { @class = "control-label col-md-2" })
        <div class="col-md-3" style="margin-top:7px">
            @Html.DropDownList("Category_Id", null, new { @class = "form-control", @disabled = "disabled" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Subject, new { @class = "control-label col-md-2" })
        <div class="col-md-10" style="margin-top:7px">
            @Html.DisplayFor(model => model.Subject, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
        <div class="col-md-10" style="margin-top:7px">
            @Html.DisplayFor(model => model.Description, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Requested_By, new { @class = "control-label col-md-2" })
        <div class="col-md-5" style="margin-top:7px">
            @Html.DisplayFor(model => model.Requested_By, new { @class = "form-control" })
        </div>

        @Html.LabelFor(model => model.Requestor_Mobile, new { @class = "control-label col-md-2" })
        <div class="col-md-3" style="margin-top:7px">
            @Html.DisplayFor(model => model.Requestor_Mobile, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Requestor_eMail, new { @class = "control-label col-md-2" })
        <div class="col-md-10" style="margin-top:7px">
            @Html.DisplayFor(model => model.Requestor_eMail, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Ticket_Status, new { @class = "control-label col-md-2" })
        <div class="col-md-4" style="margin-top:7px">
            @Html.EnumDropDownListFor(model => model.Ticket_Status, new { @class = "form-control" })
        </div>

        @Html.LabelFor(model => model.Periority_Id, new { @class = "control-label col-md-2" })
        <div class="col-md-4" style="margin-top:7px">
            @Html.EnumDropDownListFor(model => model.Periority_Id, new { @class = "form-control", @disabled = "disabled" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Location_Id, new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("Location_Id", null, new { @class = "form-control", @disabled = "disabled" })
            @Html.ValidationMessageFor(model => model.Location_Id)
        </div>

        @Html.LabelFor(model => model.Assigned_To, new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.DropDownList("Assigned_To", null, new { @class = "form-control", @disabled = "disabled" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-10">
            @Html.Label("Defects")
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <table class="table table-hover">
                <tr>
                    @{
                        int cnt = 0;
                        List<MaintCare.ViewModels.TicketDefectsViewModel> defects = ViewBag.Defects;
                        foreach (var defect in defects)
                        {
                            if (cnt++ % 3 == 0)
                            {
                                @:</tr><tr>
                            }
                            @:<td>
                <input type="checkbox"
                       name="selectedDefects"
                       value="@defect.Malfunction_Id"
                       @(Html.Raw(defect.IsSelected ? "checked=\"checked\"" : "")) />
                                @defect.Malfunction_Name_e

                                @:</td>
                        }
                        @:</tr>
                    }
                </table>
            </div>
        </div>

        <br />
        <div class="form-group">
            <div class="col-md-2">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
   @Html.ActionLink("Back to List", "Index")
</div>
//获取:/Tickets/Edit/5
公共行动结果修复(int?id)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var票证=db.nt\U票证
.Include(t=>t.DefectsList)。其中(t=>t.Ticket\u Id==Id)
.Single();
如果(票证==null)
{
返回HttpNotFound();
}
普选缺陷(票证);
//为清晰起见,删除了其他代码
回程票;
}
私人无效已填充已选缺陷(新界大学门票)
{
int分类;
if(Tickets.Category_Id==2)
类别ID=1;
else categoryId=票证。类别\u Id;
var allDefects=(从数据库中的m.sys\u故障
其中m.Category_Id==categoryId
选择m.ToList();
var ticketDefects=newhashset(Tickets.DefectsList.Select(t=>t.disfunction_Id));
var viewModel=新列表();
foreach(所有缺陷中的var缺陷)
{
viewModel.Add(新的TicketDefectsViewModel
{
故障识别号=缺陷。故障识别号,
故障名称=缺陷。故障名称,
IsSelected=ticketDefects.Contains(缺陷、故障\u Id)
});
}
ViewBag.Defects=viewModel;
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果修复(int?id,字符串[]selectedDefects)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var ticketToUpdate=db.nt\u票据
.包括(i=>i.DefectsList)
.其中(i=>i.Ticket\u Id==Id)
.Single();
如果(TryUpdateModel)(票证更新,“,
新字符串[]{Ticket_Id、Project_Id、Category_Id、Subject、Workshop_Id、Requested_By、requester_Mobile、requester_eMail、Location_Id、Ticket_Status、Periority_Id、Assigned_To、Description、Issue_Date、Created_By、Created_Date_、Updated_By、Updated_Date”})
{
ticketotupdate.Updated_By=User.Identity.Name;
ticketupdate.Updated\u Date=DateTime.UtcNow;
db.Entry(ticketotupdate).State=EntityState.Modified;
SetTicketDefects(selectedDefects,TicketUpdate);
尝试
{
db.SaveChanges();
返回操作(“索引”);
}
catch(DbEntityValidationException dbEx)
{
异常raise=dbEx;
foreach(dbEx.EntityValidationErrors中的var validationErrors)
{
foreach(validationErrors.validationErrors中的var validationError)
{
string message=string.Format(“{0}:{1}”,
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
如果(!string.IsNullOrEmpty(消息))
ViewBag.errorMessage=消息;
}
返回视图(“错误”);
}
抛升;
}
}
填充所选缺陷(票证更新);
返回视图(“索引”);
}
私有void SetTicketDefects(字符串[]selectedDefects,Tickets ticketToUpdate)
{
如果(selectedDefects==null)