我是网络应用程序编程的新手,已经被分配来清理一些没有人真正记得的遗留代码。
部分原因是添加新功能以取消提交费用。我设置了easy函数并添加了一个指向它的按钮。
点击后,按钮会导致编辑功能,未提交按钮中没有提及。
以下是取消提交按钮的代码。
<a href="@(Url.Action("Unsubmit",new {id=item.ID}))">UnSubmit</a>
并且取消提交功能:
public ActionResult Unsubmit(ExpenseItem expenseItem)
{
var original = ExpenseItemManager.Get(expenseItem.ID);
string receiptData = Request.Form["receiptData"];
expenseItem.Status = 0;
if (User.ExpenseIdentity().IsAdmin || original.UserId == User.ExpenseIdentity().Id || (expenseItem.CreditCardID.HasValue && expenseItem.CreditCard.UserID == User.ExpenseIdentity().Id))
return HandleSaveRequests(expenseItem, receiptData);
else
return new HttpUnauthorizedResult(string.Format("{0} does not have access to edit this expense item.", User.Identity.Name));
}
编辑声明:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Edit(ExpenseItem expenseItem){...
它永远不会进入未提交的功能,我做错了什么,它决定去哪里?
答案 0 :(得分:1)
在@Url.Action(...)
您没有指定控制器,而您应该使用expenseId
而不是id
。
您的Unsubmit
操作也期望传递ExpenseItem
个对象而不是ID。我们也会在下面更改签名。
它应如下所示(将HomeController
替换为您所使用的控制器):
<a href="@(Url.Action("Unsubmit", "HomeController", new { expenseId = item.ID }))">UnSubmit</a>
将您的ActionResult
签名更改为:
public ActionResult Unsubmit(int expenseId)
答案 1 :(得分:0)
您明显的问题是参数类型不匹配。
尝试以下签名:
public ActionResult Unsubmit(int id)
由于您在提交中指定您提交的参数名称为id。
答案 2 :(得分:0)
Url.Action构造函数之一是:
Url.Action(string actionName, string controllerName , Parameters)
尝试使用您的控制器名称;
<a href="@(Url.Action("Unsubmit",ControllerName,new {id=item.ID}))">UnSubmit</a>
另外,您要声明两个不同的变量名称id和expenseItem,您需要使用相同的变量名称id
或expenseItem
(我更喜欢这个或甚至更好的命名约定)