【问题标题】:Acknowledge and Reload PartialView with Razor使用 Razor 确认并重新加载 PartialView
【发布时间】:2012-03-18 02:44:37
【问题描述】:

我是 MVC3 和 Razor 的新手。

我在母版页上有一个“注意横幅”作为部分视图,我想通过单击链接来“确认”以关闭横幅(无需重新加载页面)。我相信我需要使用 jQuery 和 Ajax 调用,但我似乎无法找到正确的组合。

这是我的 _Layout.cshtml 的一部分:

<section id="main">
    <span id="attentionBar">@{ Html.RenderPartial("_AttentionBarPartial"); }</span>
    @RenderBody()
</section>

这是我的部分视图(现在只是使用 Session 作为快捷方式来让它工作)。我不确定用什么作为“链接”来重新加载视图:

    @{ this.Layout = null;}
    @if(! String.IsNullOrWhiteSpace(@Session["Attention"].ToString()))
    {    
        <div class="attentionPanel">
            <span class="attentionLabel">Attention</span>
            @Session["Attention"].ToString()
            <span class="attentionLabel">
@*   WHAT DO I PUT HERE *@
                @Ajax.ActionLink("X", "AcknowledgeAttentionBar", "Home", new AjaxOptions{ UpdateTargetId="attentionPanel", InsertionMode=InsertionMode.Replace })
            </span>              
        </div>
    }

这是我的 Home 控制器。同样,我不确定代码是否完全正确,但基本上我会清除显示注意横幅的条件。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Home Controller Updated At " + DateTime.Now.ToLongDateString()
            + " " + DateTime.Now.ToLongTimeString();

        return View();
    }

    public PartialViewResult AcknowledgeAttentionBar()
    {
        Session["Attention"] = String.Empty;
        return PartialView("_AttentionBarPartial");
    }
}

【问题讨论】:

  • 我还没有看到你的设计。但是,注意横幅很少是一个好的设计组件。

标签: asp.net asp.net-mvc-3 razor partial-views


【解决方案1】:

两件事:

  1. 确保您已将jquery.unobtrusive-ajax.js 脚本包含到您的页面中,以便Ajax.ActionLink 助手工作并在单击链接而不是正常重定向时发送AJAX 请求:

    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    
  2. 在您的AjaxOptions 中,您指定了UpdateTargetId="attentionPanel",但您的标记中没有带有id="attentionPanel" 的元素。你有一个带有class="attentionPanel" 的div,但这不是一回事。另一方面,您已将横幅包装在 &lt;span id="attentionBar"&gt; 中,因此您的意思可能是 UpdateTargetId="attentionBar"

【讨论】:

  • 谢谢 - 做到了。 #1 是我错过的大事,而 #2 只是我过于担心大局而错过了简单的细节。
猜你喜欢
  • 2013-08-06
  • 1970-01-01
  • 2012-09-19
  • 2012-06-17
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 2018-07-26
相关资源
最近更新 更多