[RESOLVED]if statement in view and calling a variable

Hi all. 

, I would like to be able to put an if statement around my actionlink in my view so If something was not filled out or something was not checked as true, it wouldn’t show the action link in my view.

Example

@if(GenApp.Value == true)

{

   @Html.ActionLink("SavePDF", "SavePDF", new { MotorSerialNumber = item.MotorSerialNumber})

}

@if(GenApp.Value == false)

{

 

}

 

But I am not entirely sure how to call anything from inside the view, meaning GenApp.Value.  I’m not sure how to define a variable in my View.

Hi Adam Jungers,

Thanks for your post.

As for the explanation: the

@if (GenApp.Value == true){

puts you inside a code block, and razor figures that you are just calling Html.ActionLink(..)

as if you were calling a function while not bothering about the returned result. Since you actually want to output the result of the Html.ActionLink as html.

Just to illustrate, the following will have the same outcome as the code above:

@if(GenApp.Value == true)
{
var url = Html.ActionLink("SavePDF", "SavePDF", new { MotorSerialNumber = item.MotorSerialNumber}); @url; }
else
{
//do something code
}

Here is a nice
Quick Reference

Hope this can be helpful.

Best Regards,

Eileen

Adam Jungers

But I am not entirely sure how to call anything from inside the view, meaning GenApp.Value

It seems that you want to use GenApp.Value in the view, but this GenApp.Value is defined in the controller. Is this correct? In that case in the controller assign this GenApp.Value to a ViewBag and use this ViewBag in the view as shown below

Controller Action

ViewBag.GenApp = GenApp.Value;

In the view

@if(ViewBag.GenApp == true)

{

   Html.ActionLink("SavePDF", "SavePDF", new { MotorSerialNumber = item.MotorSerialNumber})

}

Leave a Reply