关于参数的看似愚蠢的查询
本文关键字:查询 于参数 参数 | 更新日期: 2023-09-27 18:26:27
我使用C#在ASP.NET MVC 4中工作,我正试图将一个ActionResult方法的参数转换为一个变量,以便在另一个方法中使用。所以我举了一个例子:
public ActionResult Index(int ser)
{
var invoice = InvoiceLogic.GetInvoice(this.HttpContext);
// Set up our ViewModel
var pageViewModel = new InvoicePageViewModel
{
Orders = (from orders in proent.Orders
where orders.Invoice == null
select orders).ToList(),
Callouts = (from callouts in proent.Callouts
where callouts.Invoice == null
select callouts).ToList(),
InvoiceId = ser,
InvoiceViewModel = new InvoiceViewModel
{
InvoiceId = ser,
InvoiceItems = invoice.GetInvoiceItems(),
Clients = proent.Clients.ToList(),
InvoiceTotal = invoice.GetTotal()
}
};
// Return the view
return View(pageViewModel);
}
我需要int-ser以某种方式成为"全局",并且它的值可用于此方法:
public ActionResult AddServiceToInvoice(int id)
{
return Redirect("/Invoice/Index/");
}
正如您在上面的return语句中看到的,我得到了一个错误,因为我没有将变量"ser"传递回Index,但我需要它与调用操作时传递给Index的值相同。有人能帮忙吗?
当您构建到该方法的链接时,您需要确保将变量ser
与它所需的任何其他参数一起传递给该方法(不清楚AddServiceToInvoice
方法中的id是否实际上是ser
参数。这假设它不是)
视图中的操作链接
@Html.ActionLink("Add Service", "Invoice", "AddServiceToInvoice", new {id = IdVariable, ser = Model.InvoiceId})
将服务添加到发票操作方法
public ActionResult AddServiceToInvoice(int id, int ser)
{
//Use the redirect to action helper and pass the ser variable back
return RedirectToAction("Index", "Invoice", new{ser = ser});
}
您需要创建一个ID为的链接
如果你正在做一个get请求,它会是这样的:
@Html.ActionLink("Add service to invoice", "Controller", "AddServiceToInvoice",
new {id = Model.InvoiceViewModel.InvoiceId})
否则,如果你想做一篇文章,你需要创建一个表单:
@using Html.BeginForm(action, controller, FormMethod.Post)
{
<input type="hidden" value="@Model.InvoiceViewModel.InvoiceId" />
<input type="submit" value="Add service to invoice" />
}