2012-07-20 104 views
1

我知道前面两个问题,它们讨论嵌套部分视图,但解决方案不适用于我的设计(这可能不是最好的,但我是不确定如何调整它)。MVC3嵌套部分视图调用Ajax.ActionLink

背景

我收集来自用户的问卷答案,并将其存储在SQL Server为XML文件上。

我有一个部分视图,它加载了一个给定用户的所有响应的表格,这个部分视图填充了类似响应日期,xml响应文档的链接,调查问卷名称,xml问卷文档的链接(问卷info从另一个表中拉出来)以及一个Ajax ActionLink,它将重定向到分析两个相关xml文档以在第二个局部视图内打印出问题和答案列表(即将响应可视化为人类可读)的操作。

第一个局部视图在表格下方包含一个div,我希望用第二个局部视图填充Ajax.ActionLink。

问题

的回答正确呈现然而局部视图被加载到一个全新的页面,而没有任何样式。

其他办法解决这一嵌套问题使用的RenderPartial(),但是我用回PartialView()

代码

第一部分视图:

 <table> 
     <thead> 
     <tr><th>headers with other info</th> 
      <th>Display(/th> 
     <tr> 
     </thead> 
     <tbody> 
     <tr><td>cells with other info</td> 
      <td>@Ajax.ActionLink("View", "DisplayResponse","HealthStatus", new { respID = item.UniqueID,qVersion=item.QuestionnaireVersion, qname = item.QuestionnaireName }, new AjaxOptions { UpdateTargetId = "responseDisp" })</td> 
     </tbody> 
     </table> 
<div id="responseDisp"></div> <--- **This is the div I wish to populate, does anyone know why it's not working?** 

DisplayResponse行动(不解析xml文档的逻辑)

public ActionResult DisplayResponse(Guid respID, int qVersion, String qname) { 
     var allResponses = ZData.Responses; 
     var response = (from r in allResponses 
         where r.UniqueID == respID 
         select r 
          ).First(); 
     //geting an XML questionnaire document 
     var questionnaireDetails = ZodiacData.Questionnaires; 
     var questionnaire = (from q in questionnaireDetails 
          where q.Name == qname && q.Version == qVersion 
          select q 
          ).First(); 
     //creating XMLDocument to read the questionnaire 
     XmlDocument xqdoc = new XmlDocument(); 
     xqdoc.LoadXml(questionnaire.Xml); 
     XmlElement qroot = xqdoc.DocumentElement; 
     ViewBag.qroot = qroot; 
     XmlDocument xrdoc = new XmlDocument(); 
     xrdoc.LoadXml(response.Raw); 
     XmlElement rroot = xrdoc.DocumentElement; 
     ViewBag.rroot = rroot; 

     return PartialView("_PrintedResponse"); 
    } 

我会很感激任何帮助!

+1

检查'jquery.unobtrusive-ajax.js'文件包含在你的观点,因为这个文件中包含有需要的js函数'@ Ajax.ActionLink' – nemesv 2012-07-20 12:53:01

+0

贵部分有型号?我没有看到你传递任何东西给部分。 – anAgent 2012-07-20 12:57:54

+0

@nemesv:谢谢!就是这样,多么尴尬。如果您想将其作为答案,我会接受它。 – Nieszka 2012-07-20 13:05:00

回答

2

在MVC3中,@AJax.助手正在呈现常规forma标签,并带有一些额外的data-属性。为了使魔术工作,需要一些Javascript,它将使用这个生成的data-属性来进行必要的jQuery ajax调用。

这些JS功能是居住在jquery.unobtrusive-ajax.js所以这行添加到您的布局或视图,它应该工作:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" 
     type="text/javascript"></script> 
1

首先,正如上面提到的,你必须对jquery.unobtrusive-参考ajax.js文件,因为这会让你的事情“正确地”连接起来。

这个答案也回应了你对你的问题的评论,关于你如何将你的模型传递给你的观点。实际上,通过在模型中使用ViewBag,可以让事情变得更加复杂。

通过在模型中使用ViewBag,您将更难找到/修复/解决错字的问题以及Razor助手的强大功能。 ViewBag是一个动态对象,没有编译时类型检查。你不需要施放你的对象(少代码)。

优选(和最佳实践)是挂钩东西像这样:

1)你的控制器包含的ViewModels(强类型),其被传递到的ViewModels

控制器

public ActionResult Something() { 
     return View(); 
    } 

    public ActionResult UserView() { 
     UserViewModel mdoel = new UserViewModel { 
      Email = "[email protected]", 
      FirstName = "Your", 
      SStatuses = new List<SStatus>{ 
       new SStatus { 
        ID = 0 
       } 
      } 
     }; 
     return PartialView("_SomethingPartial", mdoel); 
    } 

指数( “东西” 视图)

@{ 
    ViewBag.Title = "Something"; 
} 

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> 
<h2>Something</h2> 
@Ajax.ActionLink("Ajax Click", "UserView", new AjaxOptions { UpdateTargetId = "MyDivContainer", InsertionMode = InsertionMode.Replace }) 
<div id="MyDivContainer"> 
<!-- my content should be here --> 
</div> 

管窥

@model StackModels.UserViewModel 
<div class="par"> 
    @Html.LabelFor(m => m.FirstName) 
    <div class="field"> 
     @Html.TextBoxFor(m => m.FirstName) 
     @Html.ValidationMessageFor(m => m.FirstName) 
    </div> 
</div> 
+0

这极大地帮助我创建一个视图模型,我希望我可以做的不仅仅是上调它,而且我不能真正将它标记为正确的答案。非常感谢你让我的代码更优雅! – Nieszka 2012-07-23 10:15:10

+0

很高兴能帮到你! – anAgent 2012-07-23 10:54:59