How can I change this ASP syntax to Razor? - asp.net-mvc-3

How can I convert this to ASP.NET?
Or how can I convert ASP to Razor, what will I add or remove and what are the things to remember while converting ASP to Razor or reverse.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PopulatingDDLusingEF.ViewModels.IndexViewModel>" %>

<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PopulatingDDLusingEF.ViewModels.IndexViewModel>" %>
In Razor you should use #Model keyword
#model PopulatingDDLusingEF.ViewModels.IndexViewModel
#{
ViewBag.Title = "Result";
Layout = "~/Views/Shared/Site.Master";
}
This is an example how to convert ASP syntax to razor syntax which i got from Marcind's post
<% if(someCondition) { %>
<ol>
<% foreach(var item in Model) { %>
<li><%: item.ToString() %></li>
<% } %>
</ol>
<% } %>
Can be expressed as follows in Razor:
#if(someCondition) {
<ol>
#foreach(var item in Model) {
<li>#item.ToString()</li>
}
</ol>
}
Also check this for a quick useful Razor reference

Related

MVC pagedlist.mvc and aspx view

I have got a page where im using the Nuget package pagedlist.mvc 3.0.18 (The latest version for MVC3). The problem is that when i'm listing the pages its makes my pages into a list of which look like
previous
1
2
3
4
next
Instead of < 1,2,3,4,5,6,7.. >
My view looks like
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IPagedList<News>>" %>
<%# Import namespace="PagedList" %>
<%# Import namespace="PagedList.Mvc" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table>
<tr>
<td>
<% foreach (var v in Model)
{%>
<%: v.Content %><br />
<%: v.DateCreated %><br />
<%: v.Email %><br />
<%} %>
<h2>News</h2>
<%: Html.PagedListPager(Model, page => Url.Action("News", new { page }), PagedListRenderOptions.Minimal)%>
Code behind looks like
public ActionResult News(int? page)
{
List<News> products = HomeBLL.GetNewsList(); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?
var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
var onePageOfProducts = products.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize
ViewBag.OnePageOfProducts = onePageOfProducts;
return View(onePageOfProducts);
}
Any suggestions on what could be wrong? The standard CSS is included as well.
You should include PagedList.css in your View (it is located in Content folder).
It seems that the newest package of pagedlist is NOT supported for the aspx views, unless you reach a low level of this package.
Include the css file PagedList.css inside HEAD tags in your _Layout page or at the top of your viewpage.

ASP.NET MVC 3 Razor view Pagination

I'm developing a pagination using MVC 3.0 with code below. All good except how do I populate re-generate the pagination numbers when user selects 5 and then clicks on Next button:
1 2 3 4 5 Next
When user clicks on Next the pagination should look like below:
Prev 2 3 4 5 6 Next
The code look like below
<ul class="pagination-clean">
<% if (ViewData.HasPreviousPage)
{ %>
<li class="previous">« Previous</li>
<% }
else
{ %>
<li class="previous-off">« Previous</li>
<% } %>
<%for (**int page = 1; page <= ViewData.TotalPages; page++**)
{
if (page == ViewData.PageIndex)
{ %>
<li class="active"><%=page.ToString()%></li>
<% }
else
{ %>
<li><%=page.ToString()%></li>
<% }
}
if (ViewData.HasNextPage)
{ %>
<li class="next">Next »</li>
<% }
else
{ %>
<li class="next-off">Next »</li>
<% } %>
</ul>
Is this the custom pagination that you have developed? Have you tried using jqGrid? It has direct binding with the controller action to retrieve the data and it's own pager control. You can explore it. Page number and other parameters will be automatically updated

What's the difference style of writing in .net's MVC2,MVC3 and Razor?

If anybody has written application in .net's MVC2 and MVC3, there is a change of syntax in writing a code like
<%= %>
has been replaced by
<%: %>
or
#
so this mean that:
<%= %> == <%: %> OR <%= %> == #
are equals?
<%= %>
Writes out the string exactly as is.
<%: %>
Html Encodes the string and then writes it out.
#
Html Encodes the string and then writes it out if you're using the Razor view engine.

Where can I find the default Object.cshtml Editor template?

I need to modify the default editor template for scaffolding but I havent found the Object.cshtml template, where can I find the default razor Object.cshtml Editor template?
The following blog post describes how to customize the editor templates: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
Basically you have to add a file named Views\Shared\EditorTemplates\Object.cshtml and put all the logic for displaying the object there.
When #marcind says they're compiled in, the templates themselves are not embedded but are rather written in code. For example, EditorFor calls TemplateFor which may call TextAreaExtensions.TextArea or one of many other extensions which generate the code that is ultimately output. This may be because the we have the option of removing the default view engine and using something like nhaml.
The mapping between the template names and the function creating the resulting output can be seen in System.Web.Mvc.Html.TemplateHelpers. See also System.Web.Mvc.Html.DefaultEditorTemplates.
The closest thing that exists right now are the Webforms templates that exist in Mvc3Futures which are available on the aspnet.codeplex.com website. Within it exists an DefaultTemplates\EditorTemplates folder that contains the templates.
Here's the Object.ascx template:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
bool ShouldShow(ModelMetadata metadata) {
return metadata.ShowForEdit
&& metadata.ModelType != typeof(System.Data.EntityState)
&& !metadata.IsComplexType
&& !ViewData.TemplateInfo.Visited(metadata);
}
</script>
<% if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
<% if (Model == null) { %>
<%= ViewData.ModelMetadata.NullDisplayText %>
<% } else { %>
<%= ViewData.ModelMetadata.SimpleDisplayText %>
<% } %>
<% } else { %>
<% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(pm))) { %>
<% if (prop.HideSurroundingHtml) { %>
<%= Html.Editor(prop.PropertyName) %>
<% } else { %>
<% if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())) { %>
<div class="editor-label"><%= Html.Label(prop.PropertyName) %></div>
<% } %>
<div class="editor-field"><%= Html.Editor(prop.PropertyName) %> <%= Html.ValidationMessage(prop.PropertyName, "*") %></div>
<% } %>
<% } %>
<% } %>

MVC Html.ActionLink ignores the Controller parameter

I have a a view that has the following code:
<h2><%= Model.Company.CompanyName %></h2>
<h3>Projects</h3>
<ul>
<%
foreach (Project p in Model.Company.Projects)
{
%>
<li><%= Html.ActionLink(p.ProjectName,"Details", "Projects", new {id=p.ProjectID,companyId=p.CompanyID}) %></li>
<%
}
%>
</ul>
<%= Html.ActionLink("Add Project", "Create", "Projects", new {id = Model.CompanyID}) %>
<br />
<h3>Users</h3>
I have a ProjectsController but when I run the application and click on the Add Project Link it expects to go to /Company/Create instead of /Projects/Create. Am I missing something?
You're matching the signature that expects the route values in the third parameter and the html attributes in the fourth. Add another parameter (null is ok) and you'll get the signature that has the link text, action, controller, route values, and html attributes.
<%= Html.ActionLink("Add Project",
"Create",
"Projects",
new {id = Model.CompanyID},
null ) %>

Resources