Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using If statement in a MVC Razor View

In the following code,

If I use "@If" statement ,I get the following compile code error as " The name 'grid' does not exist in the current context.

@if (Model.SModel != null)

{

@{ 
    WebGrid grid = new WebGrid(Model.SModel);

 }

 }

 else
 {
}

@grid.GetHtml()

,

But the code compiles without the "If" statement.For example

@{ 
    WebGrid grid = new WebGrid(Model.SModel);

}
@grid.GetHtml().

What is the syntactical error in using If else statement

like image 709
user2630764 Avatar asked Aug 05 '13 16:08

user2630764


2 Answers

grid isn't declared outside of the scope of your if statment.

Try this instead:

@if (Model.SModel != null) {
    WebGrid(Model.SModel).GetHtml()
}
like image 140
hunter Avatar answered Oct 07 '22 10:10

hunter


I would try this:

@if (Model.SModel != null)
{
    WebGrid grid = new WebGrid(Model.SModel);
    grid.GetHtml()
}
else
{
}
like image 25
Brian Maupin Avatar answered Oct 07 '22 09:10

Brian Maupin