【问题标题】:Display Dynamic Content in ASP.NET MVC 3在 ASP.NET MVC 3 中显示动态内容
【发布时间】:2014-06-11 01:07:34
【问题描述】:

我有以下结构:

  1. Site.Master

  2. 首页 - 视图

  3. HomeController - 控制器

在 Site.Master 中,我有一个包含多个 ActionLink 的标题,其中一个是常见问题解答。在主页视图中,我有基本上显示静态内容的 HTML,但是,在中心窗格/div 中,我希望有动态内容,基于用户单击的某些 HTML.ActionLinks。因此,例如,最初,我希望中心 DIV 显示介绍 - 但如果用户单击我的 Faq ActionLink,我希望中心 DIV 显示特定于我的 Faq 的内容。

在 HomeController 我有以下内容:

[HttpGet]
    public ActionResult Intro()
    {
        var introRequest = _gatewayService.GetContent(new GetContentRequest { Content = ContentTypes.Introduction });

        ViewData["content"] = introRequest.Result;

        return View();
    }

 [HttpGet]
    public ActionResult Faq()
    {
        var faqRequest = _gatewayService.GetContent(new GetContentRequest { Content = ContentTypes.Faq });

        ViewData["content"] = faqRequest.Result;

        return View();
    }

这个想法是常见问题的操作链接看起来像:

<%= Html.ActionLink("Faq","Faq","Home") %> 

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    正如@Valamas 所说,使用Ajax.ActionLink。例如,
    在标记中:

    <div id=”faqContent”>
       @Ajax.ActionLink(“Click here to see FAQ!”,
          “Faq”,
          new AjaxOptions{
             UpdateTargetId=”faqContent”,
             InsertionMode=InsertionMode.Replace,
             HttpMethod=”GET”
          })
    </div>
    

    在控制器中:

    public ActionResult Faq()
    {
        var faqRequest = _gatewayService.GetContent(new GetContentRequest { Content = ContentTypes.Faq });
    
        return PartialView("Faq", faqRequest.Result);
    }
    

    最后有一个部分视图 Faq.chtml 以及常见问题解答所需的 html。

    【讨论】:

      【解决方案2】:

      首先你需要在你的控制器中改变你的action方法的结果,而不是actionresult你可以使用jsonresult 二、在视图中可以使用jquery来加载动态内容

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-16
        • 2020-11-22
        • 2016-08-05
        • 2012-03-20
        • 2010-10-02
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        相关资源
        最近更新 更多