C# 使用MVC+;ajax(jquery)加载页面内容,aspx或ascx或两者
我希望有一个菜单,当我单击时,它会用mvc视图中的内容替换“main”div的内容。如果我使用一个.aspx页面,这个效果很好,但是任何master.page内容都会加倍(如和任何css/js)。如果我也这样做,但使用.ascx用户控件,则加载内容时不带额外内容,但如果任何浏览器直接加载菜单项(即搜索机器人或禁用JS的人),则显示的页面将不带master.page内容 到目前为止,我找到的最佳解决方案是将内容创建为.ascx页面,然后如果直接从菜单链接调用,则使用.aspx页面加载该内容,而ajax javascript将修改该链接以仅使用.ascx。但这会导致大量重复,因为每个用户控件都需要自己的.aspx页面C# 使用MVC+;ajax(jquery)加载页面内容,aspx或ascx或两者,c#,jquery,model-view-controller,.net-4.0,design-patterns,C#,Jquery,Model View Controller,.net 4.0,Design Patterns,我希望有一个菜单,当我单击时,它会用mvc视图中的内容替换“main”div的内容。如果我使用一个.aspx页面,这个效果很好,但是任何master.page内容都会加倍(如和任何css/js)。如果我也这样做,但使用.ascx用户控件,则加载内容时不带额外内容,但如果任何浏览器直接加载菜单项(即搜索机器人或禁用JS的人),则显示的页面将不带master.page内容 到目前为止,我找到的最佳解决方案是将内容创建为.ascx页面,然后如果直接从菜单链接调用,则使用.aspx页面加载该内容,而aj
我想知道有没有更好的方法?例如,如果使用参数?ajax=true?调用master.page,它是否可以隐藏.aspx页面中未包含的所有内容?制作一个ActionMethod,根据它收到的http请求类型更改它呈现的内容呢?因此,如果是ajax请求,它将呈现ascx,但如果不是,那么它可以呈现整个视图(或者重定向到呈现整个视图的另一个操作) 差不多
public ActionResult Section1()
{
if (Request.IsAjaxRequest())
{
return PartialView("section1.ascx");
}
return View("section.aspx");
}
我猜section.aspx可以包含一个RenderPartial(section 1.ascx)(这样你就不用做两次页面了)。下面是一个我非常成功地使用的方法示例: 他认为:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Namespace.Stuff>>" %>
<asp:Content ID="Content3" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
$(document).ready(function(){
$("#optionsForm").submit(function() {
$("#loading").dialog('open');
$.ajax({
type: $("#optionsForm").attr("method"),
url: $("#optionsForm").attr("action"),
data: $("#optionsForm").serialize(),
success: function(data, textStatus, XMLHttpRequest) {
$("#reports").html(data); //replace the reports html.
$("#loading").dialog('close'); //hide loading dialog.
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#loading").dialog('close'); //hide loading dialog.
alert("Yikers! The AJAX form post didn't quite go as planned...");
}
});
return false; //prevent default form action
});
});
</script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<div id="someContent">
<% using (Html.BeginForm("Index", "Reports", FormMethod.Post, new{ id = "optionsForm" }))
{ %>
<fieldset class="fieldSet">
<legend>Date Range</legend>
From: <input type="text" id="startDate" name="startDate" value="<%=ViewData["StartDate"] %>" />
To: <input type="text" id="endDate" name="endDate" value="<%=ViewData["EndDate"] %>" />
<input type="submit" value="submit" />
</fieldset>
<%} %>
</div>
<div id="reports">
<%Html.RenderPartial("ajaxStuff", ViewData.Model); %>
</div>
<div id="loading" title="Loading..." ></div>
</asp:Content>
我们通过使用所有控制器都继承自的baseController类,并对OnActionExecuted使用重写,解决了这个问题:
/// <summary>
/// Changes the masterpage to a slim version in AjaxRequest
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
var action = filterContext.Result as ViewResult;
if (action != null && Request.IsAjaxRequest())
{
action.MasterName = "Ajax";
}
base.OnActionExecuted(filterContext);
}
//
///在AjaxRequest中将母版页更改为精简版本
///
///
受保护的覆盖无效OnActionExecuted(ActionExecutedContext筛选器上下文)
{
var action=filterContext.Result作为ViewResult;
if(action!=null&&Request.IsAjaxRequest())
{
action.MasterName=“Ajax”;
}
base.OnActionExecuted(filterContext);
}
“Ajax”母版页是一个简单的母版页,只有一个contentPlaceHolder。只要所有可以用ajax调用的aspx页面都只使用这个占位符,这就可以正常工作 这正是我在上面描述的解决方案,正如我所说,我发现它不够完美,因为我必须为每个.ascx page.bangoker allready提供一个.aspx页面,以完全相同的方式回答,使用的代码要少得多,还有一个问题是,每个视图都必须有一个单独的.aspx页面。我给你的解决方案是一个aspx和一个ascx,它在初始加载时呈现,并在回发时直接接收数据。通过使ascx的名称成为ajax请求中发送的表单字段,可以轻松地将其扩展到一个具有多个ascx控件的aspx,这些控件将在if Request.IsAjaxRequest中动态加载。我们使用这种方法取得了巨大成功。抱歉,代码太多了…我只是想给你一个端到端的例子。
/// <summary>
/// Changes the masterpage to a slim version in AjaxRequest
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
var action = filterContext.Result as ViewResult;
if (action != null && Request.IsAjaxRequest())
{
action.MasterName = "Ajax";
}
base.OnActionExecuted(filterContext);
}