ASP.NET MVC:重定向从一个视图到另一个 [英] ASP.NET MVC: Redirecting from one view to another

查看:528
本文介绍了ASP.NET MVC:重定向从一个视图到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我现在用的是下面的code从剑道格重定向到另一个页面(视图):

  @(Html.Kendo()网格和LT。WM.ViewModels.StockReceiptsGridViewModel>()
    。名称(网格)
    .ToolBar(工具栏=> toolbar.Template(<一类='K-按钮K-按钮icontext的onclick ='addMaterialPopup()'的href ='#'>< / SPAN>创建股票收据< / A>中))
    .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 =/图片/索引Sto​​ckReceiptID =#= StockReceiptID#?>#=的getText()#&下; / A>中);
        columns.Command(命令=方式> command.Custom(编辑)点击(editreceipt));    })
    .DataSource(数据源=>数据源
        阿贾克斯()
        .Batch(假)
        。型号(型号=> model.Id(P => p.StockReceiptID))
                    .Read(读取=方式> read.Action(读,StockReceiptsGrid)数据(ExtraData))
  )

的Javascript:

 函数editreceipt(五){
    亦即preventDefault();
    VAR的DataItem = this.dataItem($(e.currentTarget).closest(TR));
    VAR stockReceiptId = dataItem.StockReceiptID;    window.location.href =@ Url.Action(更新,StockReceipt)+/+ stockReceiptId; //问题code ...    }

在StockReceipt控制器接收方法是:

 公众的ActionResult更新(INT stockReceiptId)
{
   VAR stockReceipt = _stockReceiptRepository.GetAllStockReceipts()了ToList()凡(R = GT; r.StockReceiptID == stockReceiptId)。    VAR模型=新StockReceiptViewModel();
    model.Notes = stockReceipt.First()注意。    返回查看(「指数」,型号);
}

这是我的路由配置:

 公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);    routes.MapRoute(
        名称:默认,
        网址:{控制器} / {行动} / {ID}
        默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
    );
}

问题:

..是,上面的Javascript code未重定向,它正在我到这个网址:HTTP: //本地主机:50439 / StockReceipt / 6

和与此错误报告的死亡黄屏':

参数词典共包含参数非可空类型'System.Int32'的方法'WorcesterMarble.Controllers.StockReceiptController'System.Web.Mvc.ActionResult更新(Int32)已的stockReceiptId的空项。一个可选参数必须是引用类型,可空类型,或声明为可选参数。
参数名:参数

其中6是ID。

如果我删除id元素变成了这个样子:

  window.location.href =@ Url.Action(更新,StockReceipt)

它的工作原理,但我需要的ID,因为我想加载所选的视图模型中的目标视图。

我不知道我做错了?!

我一直在使用这种尝试,但都无济于事:

  window.location.href = @ Url.RouteUrl(默认,新{@Controller =StockReceipt,@Action =更新})+'//'+ stockReceiptId;


解决方案

解决这样的:

 函数editreceipt(五){
    亦即preventDefault();
    VAR的DataItem = this.dataItem($(e.currentTarget).closest(TR));
    VAR stockReceiptId = dataItem.StockReceiptID;    window.location.href =@ Url.Action(更新,StockReceipt)stockReceiptId =?+ stockReceiptId;
    }

更多细节这里

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:

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:

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

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

Where 6 is the ID.

If I remove the id element to become like this:

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.

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;

解决方案

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

这篇关于ASP.NET MVC:重定向从一个视图到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆