Reputation: 1
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>
Upvotes: 0
Views: 90
Reputation: 13248
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 />
Upvotes: 0
Reputation: 34
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>
Upvotes: 0
Reputation: 3180
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 :)
Upvotes: 2
Reputation: 151644
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.
Upvotes: 4