我正在使用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/");
}
正如您在上面的返回语句中所看到的,我得到一个错误,因为我没有将变量“ser”传递回Index,但是我需要它是与行为时传递给Index的值相同调用。有人可以帮忙吗?
答案 0 :(得分:1)
当您构建指向该方法的链接时,您需要确保将变量ser
与其所需的任何其他参数一起传递给方法(不清楚{{1}中的id是否为{{1} }方法实际上是AddServiceToInvoice
参数。这假设它不是)
观看行动链接
ser
AddServiceToInvoice操作方法
@Html.ActionLink("Add Service", "Invoice", "AddServiceToInvoice", new {id = IdVariable, ser = Model.InvoiceId})
答案 1 :(得分:0)
您需要创建一个包含该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" />
}