在MVC 3上使用嵌套模型:如何更新表中的表? (外键)

时间:2012-11-19 23:18:57

标签: c# asp.net asp.net-mvc-3 entity-framework razor

所以我对ASP.NET MVC3编程比较陌生,特别是因为实体框架代码首先创建和管理数据库。

所以,我正在研究一种完整的营养系统,必须让营养师对患者进行评估。首先,系统允许建立一个apointment,同时采用营养师的ID,患者和日期时间。一旦约会结束,Nitritionist必须能够更新约会网,并包括营养师评估表,其中包括允许患者吃多少。

所以,我的课程如下

模型: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; } 
}

型号: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; }
}

如您所见,NutritionalValuation是PatientAppointment的一个领域。

查看: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)

(在此之前,已创建PatientAppointment,并且已填写IDClient和IDNutritionist字段。)

    <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非常聪明,可以将NutritionalEvaluation渲染为另一组字段,并在视图中包含该类的所有字段。我的问题是,当我尝试提交表单时(技术上,它应该允许更改日期,添加注释,然后添加NutritionalEvaluation字段)所有内容,但NutritionalEvaluation保存到表格中。它保持为null,而NutritionalValue表仍为空。我该怎么办?

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();
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

我没有在这台PC上安装Visual Studio,所以我无法测试这个......

假设您使用的是实体框架代码优先方法,则您的NutritionalEvaluation类与PatientAppointment类没有外键关系。假设存在1-1关系,请将NutritionalEvaluation更改为:

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; }
}