I want to create a web application using the "Single Page Interface" using ASP.NET MVC.
I searched if it was at least possible, and I think the answer is: not in an easy way (reading http://msdn.microsoft.com/en-us/magazine/cc507641.aspx#S2 second- the last paragraph, however this article is from May 2008).
I found other examples that implemented this by encoding / hacking using jQuery. However, I am looking for a clean solution using standard .NET approaches, if possible.
What I want is exactly the same functionality when you create a new “MVC web application”. However, instead of links to "/ Home / About", which reloads the entire page, I need links to " # Home / About", which downloads only the new part via AJAX.
The standard approach for calling templates (partial views) using Html.RenderPartial is exactly what I want, only then loading them through AJAX requests.
Of course, it is possible that I cannot use these templates, which for some reason are visualized by the master page (perhaps he expects that he will always be called in a certain context from a certain place or so). But maybe there is another clean decision on how to create template pages and extract them from the main page.
Who has a good solution to implement such a thing, a single page interface?
PS: I am developing in Visual Web Developer 2008 Express Edition with MVC 1.0 installed, in C #
[]
, , jQuery , .
, Html.ActionLink, anchor-links (with #), , AJAX html ( div # PartialView):
$("a").each(function() {
$(this).click(function() {
$("div#partialView").load($(this).attr("href") + " div#partialView");
location.hash = $(this).attr("href");
return false;
});
});
.
whole, . ; html :
public ActionResult About()
{
return View();
return View("About");
return PartialView();
return PartialView("About");
}
, (.. Home/About.aspx)?
, POSTing AJAX (, "requesttype = ajax" ), , AJAX ; (.. //, # / ).
Global.asax.cs, , AJAX-, ? ( .)
[edit2]
: About.ascx(UserControl) HTML- . About.aspx MasterPageFile="~/..../Site.master", HTML.
:
public ActionResult About()
{
return Request.IsAjaxRequest() ? (ActionResult)PartialView() : View();
}
PartialView (.ascx) View (.aspx), (About.aspx, ).
Global.asax.cs PartialView() View():
protected void Application_Start()
{
foreach (WebFormViewEngine engine in ViewEngines.Engines.Where(c => c is WebFormViewEngine))
{
engine.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.ascx" };
engine.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.aspx", "~/Views/Shared/{0}.aspx" };
}
RegisterRoutes(RouteTable.Routes);
}