Nyprez Nyprez - 1 month ago 18
C# Question

How to pass bool value inside ActionLink (from view to controll)?

I've to pass a bool inside ->

"@Html.ActionLink(" ", "Like", new { id = Model.Id @*, Pass bool value here *@}".


My method Like signature takes a
Guid Id
and a
bool
in parameter.

I tried with with following:

bool passBool = true;
@Html.ActionLink("Like!", "Like", new { id = Model.Id, passBool}
@Html.ActionLink("Disike!", "Like", new { id = Model.Id, passBool = false}


But I receive it as null.

public ActionResult Like(Guid id, bool? getBool) //gets null error without '?'
{
if (getBool == true){

return Content("Liked!");

}else if (getBool == false){

return Content("Disiked!");
}


I also tried to fix it trough a view model without success:

var liked = new Test.ViewModel.LikeViewModel {LikeDislike = true};

@Html.ActionLink("Like!", "Like", new { id = Model.Id, liked}


But it doesn't work since these bool values are still null when I pass them. Any idea how to pass a bool value from view to controller?

jle jle
Answer

You need to name the parameter you are passing so that it matches your controller action:

@Html.ActionLink("Like!", "Like", new { id = Model.Id,getBool= passBool}
Comments