0

I'm working in a HR project in ASP.NET MVC using Entity Framework with a database-first approach.

My view markup is:

<div class="form-group">
    @Html.LabelFor(model => model.emp_Disease, htmlAttributes: new { @class = "control-label col-md-2 " })
    <div class="col-md-10" style="padding-top:6px;margin:0px;">
        @Html.CheckBoxListFor(m => m.emp_Disease,m=> m.Disease,s => s.Name, s => s.Name,s => s.emp_Disease,MvcCheckBoxList.Model.Position.Horizontal)
    </div>
</div>

Controller action is:

[HttpPost]
public ActionResult Home(tbl_employee emp)
{
    using (hrm_DB db = new hrm_DB())
    {
        if (emp.emp_Id == 0)
        {
            db.tbl_employee.Add(emp);
            db.SaveChanges();

            TempData["SaveMessage"] = "Save Record Succesfully! ";
        }
        else
        {
            db.Entry(emp).State = EntityState.Modified;
            db.SaveChanges();

            TempData["EditMessage"] = "Change Succesfully! ";
        }
    }

    return RedirectToAction("ViewAll");
}

My model classes are:

public partial class tbl_employee
{
     [DisplayName("AnyDisease")]
     public string[] emp_Disease { get; set; }
     public List<Checkdisease> Disease { get; set; }
}

public class Checkdisease
{
        public int id { get; set; }
        public string Name { get; set; }
        public bool emp_Disease { get; set; }
}

Only store the one value. My problem is not submit the array store in database. How to store the string[] in database model ?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Rehmat Ali
  • 51
  • 8
  • it depends on what database structure you are using, or you can save string value as comma saparated in one column using foreach on array property. – vishu minhas Nov 22 '17 at 07:06
  • You do not store a `string[]` in a database field. You have a separate 1-many table with a relationship to your `tbl_employee` table –  Nov 22 '17 at 07:06
  • And before you consider a csv, read [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) –  Nov 22 '17 at 07:07
  • how to use comma seprated using foreach on array property please hint in the comment so that eassily handle this problem assign the link – Rehmat Ali Nov 22 '17 at 07:20

0 Answers0