MVC4 - ContextDependentView - 这是什么意思? [英] MVC4 - ContextDependentView - What does it mean?
问题描述
我刚开始使用MVC4,而且我看一下有没有新的东西的第一个动作方法。我检查了互联网,找不到这事:
I have just started using MVC4 and the first action method that I look at has something new. I checked out the internet and can't find anything about this:
public ActionResult LogOn()
{
return ContextDependentView();
}
有谁知道ContextDependentView是什么?
Does anyone know what a ContextDependentView is ?
新的东西给我。
推荐答案
它的目的是为了便于登录查看或PartialView行动的结果和注册行为。
It's purpose is to facilitate View or PartialView action results for the Login and Register actions.
private ActionResult ContextDependentView()
{
string actionName = ControllerContext.RouteData.GetRequiredString("action");
if (Request.QueryString["content"] != null)
{
ViewBag.FormAction = "Json" + actionName;
return PartialView();
}
else
{
ViewBag.FormAction = actionName;
return View();
}
}
就像MVC其他的事情是按约定完成......这里的约定是,当的Request.QueryString
包含?内容= XXXX
,它prepends的Json的动作名称,填料它ViewBag属性,并返回查看的部分版本。例如:
Like other things in MVC it is done by convention... the convention here is when the Request.QueryString
contains a ?content=xxxx
, it prepends "Json" to the action name, stuffs it a ViewBag property and returns the partial version of the View. For example:
一个用于 /帐号/登录含量=测试
将被解析为 ViewBag.FormAction =JsonLogin的要求;
然后返回部分。
A request for /Account/Login?content=test
would be resolved to ViewBag.FormAction = "JsonLogin";
and then return a partial.
一个用于请求 /帐号/登录
没有内容的查询字符串因此其形式仍然行动 ViewBag.FormAction =登陆;
A request for /Account/Login
has no content query string so its form action remains ViewBag.FormAction = "Login";
这篇关于MVC4 - ContextDependentView - 这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!