将复选框与文本框关联

时间:2018-08-30 02:15:57

标签: c# asp.net-mvc ef-code-first

我有一个有效的复选框代码,它已正确保存在数据库中,但是突然之间,他们希望为每个复选框都添加一个备注/文本框,我能够用复选框填充文本框,但未保存在数据库中。我该怎么办?

这是我的模型:ServiceRequestForm,清单为M:M

 public ServiceRequestForm()
    {
        this.checkLists = new HashSet<checkList>();
    }


 public class checkList
    {
    public checkList()
    {
        this.ServiceRequestForms = new HashSet<ServiceRequestForm>();
    }
    public int checkListId { get; set; }
    public string checkList1 { get; set; }
    public virtual ICollection<ServiceRequestForm> ServiceRequestForms { get; set; }
    }

 ** VIEW MODEL// NOT MAPPED IN THE DATABASE**
 public class srfChecklistVM
   {
    [Key]
    public int checkListId { get; set; }
    public string checkList1 { get; set; }
    public bool Assigned { get; set; }
    public string remarks { get; set; }
   }

我想要的是每个复选框都有一个文本框,并将其成功保存到数据库中。

我的控制器:

 public ActionResult Create(ServiceRequestForm serviceRequestForm, string[] selectedChecklist)
    {
        if (selectedChecklist != null)
        {
            serviceRequestForm.checkLists = new List<checkList>();
            foreach (var check in selectedChecklist)
            {
                var checkToAdd = db.checkLists.Find(int.Parse(check));
                serviceRequestForm.checkLists.Add(checkToAdd);
            }
        }
            db.ServiceRequestForms.Add(serviceRequestForm);
            db.SaveChanges();
            PopulateAssignedData(serviceRequestForm);
     }

     //Populate checklist
    private void PopulateAssignedData(ServiceRequestForm servicerequestform)
    {
        var allchecklist = db.checkLists;
        var srfchecklist = new HashSet<int>(servicerequestform.checkLists.Select(b => b.checkListId));
        var viewModel = new List<srfChecklistVM>();
        foreach (var check in allchecklist)
        {
            viewModel.Add(new srfChecklistVM
            {
                checkListId = check.checkListId,
                checkList1 = check.checkList1,
                Assigned = srfchecklist.Contains(check.checkListId)
            });
        }
        ViewBag.checkLists = viewModel;
    }

HTML

<tr>
@{
  <td colspan="2"><a href="#" class="toggler" data-prod-cat="1"><i class="fa fa-window-maximize" id="expand"></i><label>&nbsp; CheckList</label></a></td>
  <td colspan="2"><label>Remarks</label></td>
  int cnt = 0;

  List<SRF.Models.srfChecklistVM> checklists = ViewBag.checkLists;
  foreach (var check in checklists)
  {
    if (cnt++ % 1 == 0)
    {
     @:</tr><tr class="cat1" style="display:none">
    }
     @:
     <td colspan="2">
     <input type="checkbox" name="selectedChecklist" value="@check.checkListId" class="largerCheckbox"
      @(Html.Raw(check.Assigned ? "checked=\"checked\"" : "")) />
     @check.checkList1
     </td>
     @:
     //textbox for remarks was populated but does not save in database
     <td colspan="2"><input type="text" name="selectedChecklist" value="@check.remarks"></td>
     }
     @:</tr>
     }

首先,我想知道新模型是什么,其次是如何填充它,最后是如何将其保存在数据库中。任何帮助将不胜感激!

0 个答案:

没有答案