简体   繁体   English

传递给后期控制器的参数始终为null

[英]Parameter passed to post controller is always null

This question is really similar to my last one but this has to do with passing multiple parameters. 这个问题确实类似于我的上一个问题,但这与传递多个参数有关。 My controller has two methods with the same name but one gets passed a nullable int to determine which action to take on post (I don't know if that's a good design decision). 我的控制器有两个名称相同的方法,但一个方法传递了一个可为null的int来确定要执行的操作(我不知道这是一个好的设计决定)。 Here's the code: 这是代码:

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();
     }
 }

the view is huge but I'll post the relevant piece: 视图是巨大的,但我将发布相关文章:

@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>
 }

and just for good measure, my routes: 出于很好的考虑,我的路线是:

    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 }
        );
    }

So the idea is that based on authentication and whether they own a specific event they are looking at, they will have different buttons show on the screen. 因此,其想法是基于身份验证以及他们是否拥有自己正在查看的特定事件,他们将在屏幕上显示不同的按钮。 This certain one is for when they want to join an event they can push a button to request an invitation. 此特定功能用于当他们想要加入活动时可以按下按钮以请求邀请。 The controller knows this because of the int value being passed back as CODE_RequestInvitation. 由于将int值作为CODE_RequestInvitation传回,因此控制器知道这一点。 The action being returned is always null. 返回的动作始终为null。 I can't figure out what's wrong. 我不知道怎么了。

You problem is the use of the parameter name action (which is conflicting with the action attribute). 您的问题是使用了参数名称action (与action属性冲突)。 Change it to another name and it will work. 将其更改为另一个名称,它将起作用。 Inspect he html of the <form> element before and after the change. 在更改前后检查<form>元素的html。

You code for generating the form is awful and the use of Int32.Parse() is pointless. 您用于生成表单的代码非常糟糕,并且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"/>
}

and post back to 然后发回

[HttpPost]
public ActionResult EventDetails(int? eventID, int? actionID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM