0

I am new to MVC. Now I am learning with VS2010, ASP.NET MVC 4.

<% %> is not working for me. It is tedious for me to use @.

If i write large code with lot of if{} then <% will be useful. Can you please help me how to write with <% %>

I need like this:

The customer id is <%= Model.Id %> <br />

The customer Code is <%= Model.CustomerCode %> <br />

<% if (Model.Amount > 100) {%>
This is a priveleged customer
<% } else{ %>
This is a normal customer
<%} %>

But I am able to use like this only:

<div>
@{
    if(Model.Amount > 70000.00) 
    {
        Response.Write("This is a Privelleged Customer"); <br />
    }
    else
    {  
        Response.Write("This is a Normal Customer"); <br />
    }
}   
The Customer Id is: @Model.Id <br />
The Customer Name is: @Model.Code <br />
</div>
Mikhail Tulubaev
  • 4,141
  • 19
  • 31
  • It's because the render engine you are using is Razor. I think when you set up the project you can choose which engine to use. You can do this after you've set it up though, see this SO post here: http://stackoverflow.com/questions/16563811/changing-view-engine-after-project-has-been-created - you will just need to change it *from* Razor to the ASPX one (a bit of Internet searching will throw up what you need) – Geoff James Oct 20 '16 at 08:21
  • Why are you using Response.Write in your Razor code? You don't need to do this. Is this why you don't like Razor? – Liam Oct 20 '16 at 08:23
  • @Liam - I think it's that the OP is used to writing in the ASPX view engine. To overcome having to use `Response.Write()` function, you could just write some HTML markup within the curly braces instead – Geoff James Oct 20 '16 at 08:24
  • You could change all of your razor code into `@if(Model.Amount > 70000.00){ This is a Privelleged Customer } else { This is a Normal Customer }` This seems a lot simplar to me than your old style MVC code – Liam Oct 20 '16 at 08:25

4 Answers4

4

Do you really think the former is more readable than the latter? I don't. Razor syntax is here to stay, so you'd better get used to it and drop everything you know about ASP.NET WebForms. Also, why are you using the archaic Response.Write()?

A Razor-like implementation of your code would look like this:

@if(Model.Amount > 70000.00)
{
    <text>This is a Priveleged Customer</text><br />
}
else
{  
    <text>This is a Normal Customer</text><br />
}

Which is pretty readable if you ask me. See ASP.NET MVC View Engine Comparison for a comparison of view engines, each providing their own syntax with according pros and cons.

If you really insist on the WebForms syntax, apply the WebFormViewEngine as explained in Changing View Engine after Project has been created.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
2

I agree with CodeCaster's answer - the Razor view engine is here to stay and is certainly more readable than ASPX.

However, if you do really need to change it, you can.

Go into Global.asax.cs and find the Application_Start() method.

In there, add the following code:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new WebFormViewEngine());

And this will revert you to using the old-style view engine.

Also, you can choose which engine to use when you first set up the project - for when you set them up in future.

Hope this helps :)

Community
  • 1
  • 1
Geoff James
  • 3,122
  • 1
  • 17
  • 36
0

you can use @: to display the text inside server tags

<div>
@{
if(Model.Amount > 70000.00) 
{
 @:This is a Privelleged Customer <br />
}
else
{  
    @:This is a Normal Customer <br />
}
}   
The Customer Id is: @Model.Id <br />
The Customer Name is: @Model.Code <br />
</div>
42HacK
  • 34
  • 8
0

You could just add another property to your model (if you are able to) and handle the logic there:

public string CustomerText
{
   get
   {
       return Amount > 70000.00 ? "This is a Priveleged Customer" : "This is a Normal Customer";
   }
}

And consume it in your view:

<text>@Model.CustomerText</text><br />
Ric
  • 12,855
  • 3
  • 30
  • 36