我正在使用带有C#的ASP.NET MVC4,并且我正在尝试将一个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服务以某种方式变得“全局”,并且它的值对于这个方法是可用的:
public ActionResult AddServiceToInvoice(int id)
{
return Redirect("/Invoice/Index/");
}正如您在上面的return语句中所看到的,我得到了一个错误,因为我没有将变量"ser“传递回Index,但我需要它与调用操作时传递给Index的值相同。有人能帮上忙吗?
发布于 2013-08-06 02:35:29
在构造到该方法的链接时,需要确保将变量ser连同它所需的任何其他参数一起传递给该方法(不清楚AddServiceToInvoice方法中的id是否真的是ser参数。这假设它不是)
视图中的操作链接
@Html.ActionLink("Add Service", "Invoice", "AddServiceToInvoice", new {id = IdVariable, ser = Model.InvoiceId})AddServiceToInvoice操作方法
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});
}发布于 2013-08-06 02:22:38
您需要使用该ID创建一个链接:
如果你正在做一个get-request,应该是这样的:
@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" />
}https://stackoverflow.com/questions/18064810
复制相似问题