Nyprez Nyprez - 2 months ago 18
C# Question

Return bool from view/ActionLink to method - Asp.Net MVC

I've to pass a bool inside ->

"@Html.ActionLink(" ", "Like", new { id = Model.Id @*, Pass bool value here *@}".
My model doesn't contain a bool property nor can I add it(database first) or replace the model with a ViewModel. Is it still possible to pass a bool value to my method somehow?

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

Can I somehow pass bool:

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


To:

public ActionResult Like(Guid id, bool getBool)
{
if (getBool == true){

return Content("Liked!");

}else if (getBool == false){

return Content("Disiked!");
}


Any idea how to pass a bool value from view to controller?

Amy Amy
Answer

You pass a bool the same way you pass your model id:

@Html.ActionLink("Like!",   "Like", new { id = Model.Id, getBool = true})
@Html.ActionLink("Disike!", "Like", new { id = Model.Id, getBool = false})

Ultimately there's nothing special about Model.Id. It's just a Guid. Values passed in don't have to come from anywhere in particular. Just pass in whatever.

Comments