【问题标题】:the razor view is not displaying a navigation property value剃刀视图未显示导航属性值
【发布时间】:2012-03-04 17:13:38
【问题描述】:

我有以下操作方法:-

[HttpPost]
        public ActionResult Create(int questionid, Answer a)
        {
            if (ModelState.IsValid)
            {
        repository.AddAnswer(a);
             repository.Save();
             return PartialView("_details",a);
            }
            return View(a);

以及以下 _details 部分视图:-

    <td>
        @Html.DisplayFor(modelItem => Model.Description)
    </td>
    <td>
        @Html.DisplayFor(modelItem => Model.Answer_Description.description)
    </td>
    <td>
     @Ajax.ActionLink("Delete", "Delete", "Answer",
    new { id = Model.AnswersID },
      new AjaxOptions
      {
          Confirm = "Are You sure You want to delete this Answer ?",
          HttpMethod = "Post",
          UpdateTargetId = Model.AnswersID.ToString()
                    })


    </td>
    </tr>

我面临的问题是 @Html.DisplayFor(modelItem =&gt; Model.Answer_Description.description) 值在 ajx 调用后不会自动显示,除非我刷新网页。那么可能是什么问题?

【问题讨论】:

  • 我看到的原因是UpdateTargetId 的值类似于modelname.answerid,但实际上答案描述的呈现ID 类似于Model.Answer_Description.description

标签: asp.net-mvc-3 razor


【解决方案1】:

我怀疑存在延迟加载问题(您提供的信息很难确定,但是)...

因为 Model.Description 似乎是一个原始(字符串)属性,但 Answer_Description.description 是一个导航属性的原始属性。

您确定 Answer_Description 已正确加载?

编辑:更准确地说:您的操作收到答案:通过时是否有 Answer_Description.description? 如果没有,您应该在返回 View 之前从数据库中获取 Answer_Description...

【讨论】:

  • 感谢您的回复,我不知道如何检查它是否加载成功,但是如果我刷新页面,Answer_Description 将成功显示...
  • 试一试:在控制器中放置一个调试点:进入“Answer a”,检查 Model.Answer_Description,查看描述。如果它为空,或者当你以这种方式调试时它工作,这是一个延迟加载问题,你应该在返回视图之前为你的答案显式加载 Answer_Description
  • 感谢您的回复,我通常使用 .Include 执行急切加载的问题,在循环对象或类似场景时;但我可以确定如何在我的案例中应用包含? BR
猜你喜欢
  • 2021-03-14
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多