Asp.net mvc 3 MVC 3如何判断从哪个视图调用控制器操作-
有没有一种方法可以告诉控制器操作是从哪个视图调用的? 例如,我想使用“ControllerContext.HttpContext.Request.PhysicalPath”,但它返回控制器操作本身所在的路径:Asp.net mvc 3 MVC 3如何判断从哪个视图调用控制器操作-,asp.net-mvc-3,view,controller,Asp.net Mvc 3,View,Controller,有没有一种方法可以告诉控制器操作是从哪个视图调用的? 例如,我想使用“ControllerContext.HttpContext.Request.PhysicalPath”,但它返回控制器操作本身所在的路径: public ActionResult HandleCreateCustomer() { // Set up the customer //..code here to setup the customer //Check
public ActionResult HandleCreateCustomer()
{
// Set up the customer
//..code here to setup the customer
//Check to see of the calling view is the BillingShipping view
if(ControllerContext.HttpContext.Request.PhysicalPath.Equals("~/Order/BillingShipping"))
{
//
return RedirectToAction("OrderReview", "Order", new { id = customerId });
}
else
{
return RedirectToAction("Index", "Home", new { id = customerId });
}
}
如果可以从固定数量的位置调用它,则可以创建一个枚举,其中每个值都对应于可以从中调用它的位置。然后,您只需将此枚举值传递到HandleCreateCustomer中,并在此基础上执行条件语句。此时我正在使用以下类型的内容: 在视图中,我使用以下方法填充TempData变量:
@{TempData["ViewPath"] = @Html.ViewVirtualPath()}
HtmlHelper方法ViewVirtualPath()位于System.Web.Mvc.Html命名空间中(与往常一样),如下所示,并返回表示视图虚拟路径的字符串:
public static string ViewVirtualPath(this HtmlHelper htmlHelper)
{
try{
return ((System.Web.WebPages.WebPageBase)(htmlHelper.ViewDataContainer)).VirtualPath;
}catch(Exception){
return "";
}
}
然后我将读取控制器中的TempData变量 我找到了另一种方法。
在控制器中,您想知道从哪个页面调用它。
我在控制器中添加了以下内容
ViewBag.ReturnUrl = Request.UrlReferrer.AbsolutePath;
然后在视图中我有一个“后退”按钮
@(Html.Kendo().Button().Name("ReturnButton")
.Content("Back to List").Events(e => e.Click("onReturn"))
.HtmlAttributes(new { type = "k-button" })
)
然后是onReturn处理程序的javascript
function onReturn(e) {
var url = '@(ViewBag.ReturnUrl)';
window.location.href = url;
}