Brenton Hawley
Brenton Hawley

Reputation: 119

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). 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. The action being returned is always null. I can't figure out what's wrong.

Upvotes: 2

Views: 1624

Answers (1)

user3559349
user3559349

Reputation:

You problem is the use of the parameter name action (which is conflicting with the action attribute). Change it to another name and it will work. Inspect he html of the <form> element before and after the change.

You code for generating the form is awful and the use of Int32.Parse() is pointless.

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

Upvotes: 3

Related Questions