Click here to Skip to main content
14,924,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
These are the classes im working with :


public partial class AttachmentFile
    {
        public string Id { get; set; }
        public string FileName_ { get; set; }
        public string Ext_ { get; set; }
        public int FileSize_ { get; set; }
        public Nullable<System.DateTime> FileDate_ { get; set; }
        public string ModuleID_ { get; set; }
        public string RecordID_ { get; set; }
        public string FileType_ { get; set; }
        public string HashCode_ { get; set; }
 
        public virtual EMRecord EMRecord { get; set; }




public partial class EMRecord
    {
   
        public string Id { get; set; }
        public string EMRPatientName_ { get; set; }
        public Nullable<System.DateTime> PatientDOB_ { get; set; }
        public System.DateTime EMRDate_ { get; set; }
        public string EMRTitle_ { get; set; }
        public string EMRSymptoms_ { get; set; }
        public string EMRDiagnosis_ { get; set; }
        public string EMRTreatment_ { get; set; }
        public string EMRDoctorName_ { get; set; }
        public string InstitutionName_ { get; set; }
        public string Restrictions_ { get; set; }
        public string Clerances_ { get; set; }
        public string PatientId_ { get; set; }
        public string ProfessionalID_ { get; set; }
        public string InstitutionID_ { get; set; }
        public string HCEID_ { get; set; }
        public string EMRecordType_ { get; set; }
 
        public virtual Patient Patient { get; set; }
 
        public virtual ICollection<EMRShare> EMRShares { get; set; }
 
        public virtual ICollection<AttachmentFile> AttachmentFiles { get; set; }



So they are in a one-to-many relationship. So whenever i create an EMRecord , it could have some attachments with it , and they should save into Attachment table into database.
The create method doesnt throw any error , just save a file in the  "~/App_Data/Upload/" folder and not in the Attachment table in database . 

This is the create method and view :


@model HFC.Models.EMRecord
 
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
<h2>Create</h2>
 
    @using (Html.BeginForm("Create", "EMRecords", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    @Html.AntiForgeryToken()
 
    <div class="form-horizontal">
        <h4>EMRecord</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRPatientName_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRPatientName_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRPatientName_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.PatientDOB_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PatientDOB_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PatientDOB_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRDate_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRDate_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRDate_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRTitle_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRTitle_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRTitle_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRSymptoms_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRSymptoms_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRSymptoms_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRDiagnosis_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRDiagnosis_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRDiagnosis_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRTreatment_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRTreatment_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRTreatment_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRDoctorName_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EMRDoctorName_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EMRDoctorName_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.InstitutionName_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InstitutionName_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InstitutionName_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.Restrictions_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Restrictions_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Restrictions_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.Clerances_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Clerances_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Clerances_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.PatientId_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PatientId_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PatientId_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.ProfessionalID_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProfessionalID_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProfessionalID_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.InstitutionID_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InstitutionID_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InstitutionID_, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <div class="form-group">
            @Html.LabelFor(model => model.HCEID_, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HCEID_, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HCEID_, "", new { @class = "text-danger" })
            </div>
        </div>
 
      
 
        <div class="form-group">
            @Html.LabelFor(model => model.EMRecordType_, "EMRecordType_", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("EMRecordType_", null, "Select", new { @class = "form-control" })
 
                @Html.ValidationMessageFor(model => model.EMRecordType_, "", new { @class = "text-danger" })
            </div>
        </div>
 
 
        <div class="form-group">
            @Html.LabelFor(model => model.AttachmentFiles, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
 
                <input type="file" name="file" value="Browse" multiple="multiple" />
 
            </div>
        </div>
 
 
 
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
 
<div>
    @Html.ActionLink("Back to List", "Index")
</div>




public ActionResult Create()
       {
           ViewBag.EMRecordType_ = new SelectList(db.EMRecords, "EMRecordType_", "EMRecordType_");
 
 
           return View();
       }
 
       // POST: EMRecords/Create
       // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
       // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
       [HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Create([Bind(Include = "Id,EMRPatientName_,PatientDOB_,EMRDate_,EMRTitle_,EMRSymptoms_,EMRDiagnosis_,EMRTreatment_,EMRDoctorName_,InstitutionName_,Restrictions_,Clerances_,PatientId_,ProfessionalID_,InstitutionID_,HCEID_,EMRecordType_")] EMRecord eMRecord)
       {
           //  if (ModelState.IsValid)
           //  {
           //       db.EMRecords.Add(eMRecord);
           //      db.SaveChanges();
           //      return RedirectToAction("Index");
           //       }
           //    ViewBag.EMRecordType_ = new SelectList(db.EMRecords, "EMRecordType_", "EMRecordType_");
 
           //  return View(eMRecord);
 
 
           if (ModelState.IsValid)
           {
               List<AttachmentFile> fileDetails = new List<AttachmentFile>();
               for (int i = 0; i < Request.Files.Count; i++)
               {
                   var file = Request.Files[i];
 
                   if (file != null && file.ContentLength > 0)
                   {
                       var fileName = Path.GetFileName(file.FileName);
                       AttachmentFile fileDetail = new AttachmentFile()
                       {
 
 
                           Id = System.IO.Path.GetFileName(file.FileName),
                           FileName_ = System.IO.Path.GetFileName(file.FileName),
                           Ext_ = Path.GetExtension(file.ToString()),
                           FileSize_ = file.ContentLength,
                           FileDate_ = DateTime.Now,
                           ModuleID_ = "EMRecord",
                 //          RecordID_ = eMRecord.Id,
 
                           FileType_ = file.ContentType,
                           HashCode_ = file.GetHashCode().ToString()
 
 
                         
 
 
 
                           
                       };
                       fileDetails.Add(fileDetail);
 
                       var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"), fileDetail.Id + fileDetail.Ext_);
                       file.SaveAs(path);
                   }
               }
 
               eMRecord.AttachmentFiles = fileDetails;
               db.EMRecords.Add(eMRecord);
               db.SaveChanges();
               return RedirectToAction("Index");
           }
           ViewBag.EMRecordType_ = new SelectList(db.EMRecords, "EMRecordType_", "EMRecordType_");
           return View(eMRecord);
       }






I appriciate every help. Thanks in regards , a lot !


What I have tried:

i discriber the problem above , all detailed.
Posted
Comments
F-ES Sitecore 7-Nov-17 9:04am
   
We don't have access to your EF configuration so we can't replicate your issue. Use the debugger to step through to see what is happening and google how to manage parent\child EF relationships for pointers.
ddgjgj 7-Nov-17 9:05am
   
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900