asp.net-mvc ASP.NET MVC:从一个视图重定向到另一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21337608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC: Redirecting from one view to another
提问by t_plusplus
Hi I am using the following code to redirect from a kendo grid to another page (View):
嗨,我正在使用以下代码从剑道网格重定向到另一个页面(视图):
@(Html.Kendo().Grid<WM.ViewModels.StockReceiptsGridViewModel>()
.Name("Grid")
.ToolBar(toolbar => toolbar.Template("<a class='k-button k-button-icontext' onclick='addMaterialPopup()' href='#'></span>Create Stock Receipt</a>"))
.Columns(columns =>
{
columns.Bound(p => p.StockReceiptID);
columns.Bound(p => p.SupplierName);
columns.Bound(p => p.Product);
columns.Bound(p => p.Dimensions);
columns.Bound(p => p.Quantity);
columns.Bound(p => p.QuantityReserved);
columns.Bound(p => p.PurchaseNumber);
columns.Bound(p => p.Cost);
columns.Bound(p => p.PhotosLink).Title("").ClientTemplate("<a href='/Photos/index?StockReceiptID=#=StockReceiptID#'>#=GetText()#</a>");
columns.Command(command => command.Custom("Edit").Click("editreceipt"));
})
.DataSource(dataSource => dataSource
.Ajax()
.Batch(false)
.Model(model => model.Id(p => p.StockReceiptID))
.Read(read => read.Action("Read", "StockReceiptsGrid").Data("ExtraData"))
)
)
Javascript:
Javascript:
function editreceipt(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var stockReceiptId = dataItem.StockReceiptID;
window.location.href = "@Url.Action("Update","StockReceipt")"+"/"+stockReceiptId; // Problem code...
}
The receiving method on the StockReceipt Controller is:
StockReceipt 控制器上的接收方法是:
public ActionResult Update(int stockReceiptId)
{
var stockReceipt = _stockReceiptRepository.GetAllStockReceipts().ToList().Where(r => r.StockReceiptID == stockReceiptId);
var model = new StockReceiptViewModel();
model.Notes = stockReceipt.First().Notes;
return View("Index", model);
}
And here is my route configuration:
这是我的路线配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The Problem:
问题:
.. is that the above Javascript code is not redirecting, it is taking me to this URL: http://localhost:50439/StockReceipt/6
.. 是上面的 Javascript 代码没有重定向,它带我到这个 URL:http://localhost:50439/StockReceipt/6
And reporting the 'Yellow Screen of Death' with this error:
并报告带有此错误的“黄屏死机”:
The parameters dictionary contains a null entry for parameter 'stockReceiptId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Update(Int32)' in 'WorcesterMarble.Controllers.StockReceiptController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
对于“WorcesterMarble.Controllers.StockReceiptController”中的方法“System.Web.Mvc.ActionResult Update(Int32)”的不可为空类型“System.Int32”的参数“stockReceiptId”,参数字典包含一个空条目。可选参数必须是引用类型、可为空类型或声明为可选参数。参数名称:参数
Where 6 is the ID.
其中 6 是 ID。
If I remove the id element to become like this:
如果我删除 id 元素变成这样:
window.location.href = "@Url.Action("Update","StockReceipt")"
It works, but I need the ID because I want to load the selected 'ViewModel' in the destination view.
它有效,但我需要 ID,因为我想在目标视图中加载选定的“ViewModel”。
I wonder what am I doing wrong?!
我想知道我做错了什么?!
I have tried using this, but to no avail.:
我试过使用这个,但无济于事。:
window.location.href = @Url.RouteUrl("Default", new { @Controller = "StockReceipt", @Action = "Update"}) + '//' + stockReceiptId;
回答by t_plusplus
Solved like this:
是这样解决的:
function editreceipt(e) {
e.preventDefault();
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var stockReceiptId = dataItem.StockReceiptID;
window.location.href = "@Url.Action("Update","StockReceipt")?stockReceiptId=" + stockReceiptId;
}
More details here
更多细节在这里
回答by Mister Epic
I believe all you are looking for is this:
我相信您正在寻找的是:
window.location.href = "@Url.Action("Update","StockReceipt", new { id = 6 })"
http://msdn.microsoft.com/en-us/library/dd505151%28v=vs.118%29.aspx
http://msdn.microsoft.com/en-us/library/dd505151%28v=vs.118%29.aspx

