这个问题与我的上一个问题非常相似,但这与传递多个参数有关。我的控制器有两个具有相同名称的方法,但是一个被传递一个可空的int来确定要在post上采取哪个动作(我不知道这是一个好的设计决定)。这是代码:
public ActionResult EventDetails(int? eventID)
{
EventDetailsViewModel model = new EventDetailsViewModel();
if (eventID != null)
{
model.afterPost = false;
model.currentAttendance = getCountAttending(eventID);
model.currentUser = getCurrentUserProfile(User.Identity.Name);
model.eventDetails = getEventDetails(eventID);
model.eventRestrictions = getEventRestrictions(eventID);
model.usersAttending = getUsersAttending(eventID);
return View(model);
}
else
{
return View();
}
}
[HttpPost]
public ActionResult EventDetails(int? eventID, int? action)
{
EventDetailsViewModel model = new EventDetailsViewModel();
if (eventID != null)
{
model.afterPost = true;
model.currentAttendance = getCountAttending(eventID);
model.currentUser = getCurrentUserProfile(User.Identity.Name);
model.eventDetails = getEventDetails(eventID);
model.eventDetails["ActionID"] = action.ToString();
model.eventRestrictions = getEventRestrictions(eventID);
model.usersAttending = getUsersAttending(eventID);
return View(model);
}
else
{
return View();
}
}
视图很大但我会发布相关文章:
@if(User.Identity.IsAuthenticated)
{
if (!Model.eventDetails["Event_Owned"].Equals("true"))
{
<div class="joinEventButton">
@if(Model.eventDetails["Event_Current"].Equals("true"))
{
<form method="post" action="@Url.Action("EventDetails", "Event", new{eventID = Int32.Parse(Model.eventDetails["EventID"]), action = Int32.Parse(Model.eventDetails["CODE_RequestInvitation"])})">
<input type="submit" value="Request Invitation" class="submitButton submitButton-green"/>
</form>
}
else
{
<button class="disabledButton disabledButton-grey">Request Invitation</button>
}
</div>
}
只是为了衡量,我的路线:
public static void RegisterRoutes(RouteCollection routes)
{
//routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
所以我的想法是,基于身份验证以及他们是否拥有他们正在查看的特定事件,他们将在屏幕上显示不同的按钮。这个特定的一个是当他们想要加入一个事件时,他们可以按一个按钮来请求邀请。控制器知道这一点,因为int值作为CODE_RequestInvitation传回。返回的操作始终为null。我无法弄清楚出了什么问题。
答案 0 :(得分:3)
您的问题是使用参数名称action
(与action
属性冲突)。将其更改为其他名称,它将起作用。在更改之前和之后检查h <form>
元素的html。
您生成表单的代码很糟糕,使用Int32.Parse()
毫无意义。
@using (Html.BeginForm("EventDetails", "Event", { eventID = Model.eventDetails["EventID"], actionID = Model.eventDetails["CODE_RequestInvitation"] }, FormMethod.Post))
{
<input type="submit" value="Request Invitation" class="submitButton submitButton-green"/>
}
并发回
[HttpPost]
public ActionResult EventDetails(int? eventID, int? actionID)