ZeranCourter
ZeranCourter

Reputation: 3

Using @Html.ActionLink to pass multiple parameters to controller

I fully expect the solution to be something simple like a small detail i forgot, but for the life of me i cannot figure out what i am doing wrong

Action Link :

 @Html.ActionLink(
        linkText: "Confirm",
        actionName: "AdminVitalBitsCD",
         controllerName: "Configuration",
         routeValues: new
         {
              Title = Model.VitalBit.Title,
              Message = Model.VitalBit.Message,
              IsActive = Model.VitalBit.IsActive,
              IsPriority = Model.VitalBit.IsPriority,
              bitName = Model.VitalBit.BitType.Name,
              Created = Model.VitalBit.Created

         },
              htmlAttributes: null
       )

Controller :

[HttpGet]
public ActionResult AdminVitalBitsCD(string Title, string Message, bool IsActive, bool IsPriority, string bitName, DateTime Created)
{ 
    return View("~/Views/Configuration/AdminVitalBits.cshtml", viewModel);
}

When i click on my link the break point is hit in my controller and no error is thrown. The problem is all of the parameters are either null or their default value (Created = {1/1/0001 12:00:00 AM}). Am i forgetting something simple?

If anything else is need let me know. Thanks in advance

Upvotes: 0

Views: 235

Answers (1)

T McKeown
T McKeown

Reputation: 12847

A much simpler way and open for extension is to send a JSON object back to the controller. You would need to create an equivalent C# POCO class and use it in the controller but it is pretty simple.

[HttpGet]
public ActionResult AdminVitalBitsCD(PocoDataClass data)
{ 
    return View("~/Views/Configuration/AdminVitalBits.cshtml", viewModel);
}

Add this class to your C# project.

public class PocoDataClass 
{
   public string Title { get; set; }
   public string Message { get; set; }
   ...
}

Upvotes: 1

Related Questions