-6ren">
gpt4 book ai didi

asp.net-mvc - ASP.NET MVC : Redirecting from one view to another

转载 作者:行者123 更新时间:2023-12-03 02:07:46 26 4
gpt4 key购买 nike

您好,我正在使用以下代码从剑道网格重定向到另一个页面( 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 Controller 上的接收方法是:

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

并报告“蓝屏死机”并出现以下错误:

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

其中 6 是 ID。

如果我删除 id 元素,就会变成这样:

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

它可以工作,但我需要 ID,因为我想在目标 View 中加载选定的“ViewModel”。

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

我尝试过使用这个,但没有成功:

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

最佳答案

这样解决:

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

关于asp.net-mvc - ASP.NET MVC : Redirecting from one view to another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com