4

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

所以,我正在研究一个完整的营养系统,它必须允许营养师评估病人。因此,首先,系统允许设置一个预约,获取营养师的 ID、患者的 ID 和日期时间。一旦预约结束,营养师必须能够更新预约网,并包括营养师评估表,其中包括允许某些患者吃多少。

所以,我的课程如下

模型: 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 将保存到表格中。它保持为空,并且 NutritionalValue 表保持为空。我能做些什么?

编辑1:

控制器: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

1 回答 1

0

我没有在这台电脑上安装 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; }
}
于 2012-11-20T02:12:39.357 回答