Nyprez Nyprez - 1 year ago 134
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
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*@}


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download