ASP.NET MVC:从一个视图重定向到另一个视图

时间:2014-01-24 16:27:44

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 kendo-grid url-redirection

您好我使用以下代码从kendo网格重定向到另一个页面(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:

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...

    }

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);
}

这是我的路线配置:

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 }
    );
}

问题:

..上面的Javascript代码没有重定向,它带我到这个URL:http://localhost:50439/StockReceipt/6

并使用此错误报告“黄色死亡屏幕”:

参数字典包含非可空类型'System.Int32'的参数'stockReceiptId'的空条目,用于'WorcesterMarble.Controllers.StockReceiptController'中的方法'System.Web.Mvc.ActionResult Update(Int32)'。可选参数必须是引用类型,可空类型,或者声明为可选参数。 参数名称:参数

其中6是ID。

如果我将id元素删除为这样:

window.location.href = "@Url.Action("Update","StockReceipt")"

它有效,但我需要ID,因为我想在目标视图中加载选定的'ViewModel'。

我想知道我做错了什么?!

我尝试过使用它,但无济于事。

window.location.href = @Url.RouteUrl("Default", new { @Controller = "StockReceipt", @Action = "Update"}) + '//' + stockReceiptId;

2 个答案:

答案 0 :(得分:7)

像这样解决:

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; 
    }

更多详情here

答案 1 :(得分:4)

我相信你所要找的就是:

 window.location.href = "@Url.Action("Update","StockReceipt", new { id = 6 })"

http://msdn.microsoft.com/en-us/library/dd505151%28v=vs.118%29.aspx