jeudi 30 octobre 2014

How to get currently used PartialView?


Vote count:

0




I am trying to check which PartialView is currently being used and depending on the string returned, I will give either give or not give a class to a specific div like this:



<div id="someDiv" class="@(ViewContext.RouteData.Values["Action"].ToString() == "About" ? "collapse" : "")">


But this always returns Index because I have an SPA and partial views, like this:



public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return PartialView();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return PartialView();
}
}


I am using sammy.js for my SPA, this is my Routing.js file:



var Routing = function (appRoot, contentSelector, defaultRoute) {

function getUrlFromHash(hash) {
var url = hash.replace('#/', '');
if (url === appRoot)
url = defaultRoute;
return url;
}

return {
init: function () {
Sammy(contentSelector, function () {
this.get(/\#\/(.*)/, function (context) {
var url = getUrlFromHash(context.path);
context.load(url).swap();
});
}).run('#/');
}
};
}


And in my layout page I have this little piece of js:



$(function () {
var routing = new Routing('@Url.Content("~/")', '#page', 'welcome');
routing.init();
});


So how can I get the correct PartialView name when the content has changed?





asked 1 min ago







How to get currently used PartialView?

Aucun commentaire:

Enregistrer un commentaire