C# 关于参数的看似愚蠢的查询
我正在使用C#在ASP.NET MVC 4中工作,我正在尝试将一个ActionResult方法的参数转换为一个变量,以便在另一个方法中使用。我举个例子:C# 关于参数的看似愚蠢的查询,c#,entity-framework,asp.net-mvc-4,C#,Entity Framework,Asp.net Mvc 4,我正在使用C#在ASP.NET MVC 4中工作,我正在尝试将一个ActionResult方法的参数转换为一个变量,以便在另一个方法中使用。我举个例子: public ActionResult Index(int ser) { var invoice = InvoiceLogic.GetInvoice(this.HttpContext); // Set up our ViewModel var pageViewModel = ne
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的值相同。有人可以帮忙吗?您需要创建一个带有该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" />
}
@使用Html.BeginForm(操作、控制器、FormMethod.Post)
{
}
构建指向该方法的链接时,需要确保将变量ser
连同它所需的任何其他参数一起传递给该方法(尚不清楚AddServiceToInvoice
方法中的id是否真的是ser
参数。假设不是)
视图中的操作链接
@Html.ActionLink("Add Service", "Invoice", "AddServiceToInvoice", new {id = IdVariable, ser = Model.InvoiceId})
添加ServiceToInvoice操作方法
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});
}
这些方法如何关联?您可以在任何示例中查看如何在视图中生成指向操作的URL。。。如果样本不可用,请在问题中发布视图的代码。