Using nested models in MVC 3: How to update a table inside a table? (Foreign keys)

So, I'm relatively new to ASP.NET MVC3 programming, especially because of how the Entity Framework Code first creates and manages the database.

So, I am working on an integrated nutrition system that should allow the nutritionist to evaluate the patient. So, firstly, the system allows you to configure the appointment, taking both the nutrition identifier, the patient and the date-time. After the appointment is over, the nutritionist should be able to update the appointment and include a dietisticValuation table, which includes how many specific patients are allowed to eat.

So my classes are as follows

Model: NutritionalEvaluation.cs

public class NutritionalEvaluation { [ScaffoldColumn (false)] public virtual int ID { get; set; } public virtual int KiloCal { get; set; } public virtual int Proteines { get; set; } public virtual int Carbs { get; set; } public virtual int Fiber { get; set; } public virtual int Fats { get; set; } public virtual int Cholesterol { get; set; } public virtual int Water { get; set; } public virtual int Alcohol { get; set; } } 

Model: PatientAppointment.cs

  public class PatientAppointment { public virtual int ID { get; set; } public virtual int IDClient { get; set; } public virtual int IDNutritionist { get; set; } public virtual DateTime Date { get; set; } public virtual String Comments { get; set; } public virtual NutritionalEvaluation Valuation { get; set; } } 

As you can see, NutritionalValuation is a PatientAppointment field.

View: EvaluateAppointment.cshtml @model NutricionApp.Models.PatientAppointment

  @{ ViewBag.Title = "Valuation"; } <h2>Valuation</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>PatientAppointment</legend> @Html.HiddenFor(model => model.ID) @Html.HiddenFor(model => model.IDClient) @Html.HiddenFor(model => model.IDNutritionist) 

(Prior to this, PatientAppointment was created and the IDClient and IDNutritionist fields were filled in.)

  <div class="editor-label"> @Html.LabelFor(model => model.Date) </div> <div class="editor-field"> @Html.EditorFor(model => model.Date) @Html.ValidationMessageFor(model => model.Date) </div> <div class="editor-label"> @Html.LabelFor(model => model.Comments) </div> <div class="editor-field"> @Html.EditorFor(model => model.Comments) @Html.ValidationMessageFor(model => model.Comments) </div> <!-- <div class="editor-label"> @Html.LabelFor(model => model.Valuation) </div> <div class="editor-field"> @Html.EditorFor(model => model.Valuation) @Html.ValidationMessageFor(model => model.Valuation) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> 

MVC is smart enough to visualize NutritionalEvaluation as yet another set of fields and include all the fields of this class inside the view. My problem is that when I try to submit a form (technically, it should allow me to change the date, add a comment, and then add the NutritionalEvaluation fields) everything, BUT NutritionalEvaluation is stored on the table. It remains zero, and the NutritionalValue table remains empty. What can I do?

EDIT1:

Controller: PatientAppointmentController.cs

  namespace NutritionApp.Controllers { [Authorize] public class PatientAppointmentController : Controller { private NutritionDB db = new NutritionDB(); 

...

  public ActionResult EvaluateAppointment(int ID) { PatientAppointment appoint = db.PatientAppointment.Find(ID); return View(appoint); } [HttpPost] public ActionResult EvaluateAppointment(PatientAppointment appoint) { if (ModelState.IsValid) { db.Entry(appoint).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(appoint); } public ActionResult ViewAppointments() { return View(); } } } 
+4
source share
1 answer

I do not have Visual Studio installed on this PC, so I could not verify this ...

Assuming you are using the Entity Framework Code First approach, your NutritionalEvaluation class has no foreign key relationship to your PatientAppointment class. Assuming a 1-1 ratio exists, change the NutritionalEvaluation to:

 public class NutritionalEvaluation { [ScaffoldColumn (false)] public virtual int ID { get; set; } // This is the name of the navigation property defined below [ForeignKey("PatientAppointment")] public virtual int AppointmentID { get; set; } public virtual int KiloCal { get; set; } public virtual int Proteines { get; set; } public virtual int Carbs { get; set; } public virtual int Fiber { get; set; } public virtual int Fats { get; set; } public virtual int Cholesterol { get; set; } public virtual int Water { get; set; } public virtual int Alcohol { get; set; } // Add navigation property to PatientAppointment public virtual PatientAppointment PatientAppointment { get; set; } } 
0
source

Source: https://habr.com/ru/post/1447094/


All Articles