In the View development process of Asp.net MVC, if you do not pay attention to it, you may write a lot of repeated code. This article introduces three methods to refactor the view code to reduce repeated code in the view.
1. motherboard page
In Asp.net MVC, the use of the motherboard page is retained. We can use the motherboard page to layout our site. Let's look at the code on the following motherboard page:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
The use of dashboard pages in Asp.net MVC is similar to that in web form. You need to define contentplaceholder and use some common HTML tags for layout. When multiple pages have the same content, it is very useful to use the motherboard page.
2. Partial
Partial is similar to the user control in web form. It is used to render a content page. The biggest advantage of using partial is that these code segments are defined in the view page, not in the code. The following is an example:
Rendering partial is very simple. We can use the renderpartial and partial methods in the parent view. The code for profiles is as follows. Use renderpartial in profiles to render the profile.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Profile>>" %>
The above Code renders a list of profiles into a table. Each row defines a partial for rendering into a row. Even if the content page cannot be shared with other views, using partial in a view can simplify and reduce the number of HTML tags. The renderpartial method requires a partial name and a model parameter. To search for a local partial file, follow the following rules:
1 <area >\< controller >\< partialname>. aspx and. ascx
2 <area> \ shared \ <partialname>. aspx and. ascx
3 \ <controller >\< partialname>. aspx and. ascx
4 \ shared \ <partialname>. aspx and. ascx
These searches are similar to searching for a view by view name. You can also use <% = html. Partial ("Profile", profile) %> to render the view. The profile file can be an ascx file, or An ASPX file if necessary. The code for profile is as follows:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Profile>" %><tr> <td> <%= Html.ActionLink(Model.Username, "Show", new{ username = Model.Username }) %> </td> <td><%= Model.FirstName%></td> <td><%= Model.LastName%></td> <td><%= Model.Email %></td></tr>
We will render profiles in the view as follows:
<% Html.RenderPartial("Profiles", Model); %>
The rendering effect is as follows:
3. Child Action
Partial is very convenient for displaying information about an existing model. However, sometimes the data source displayed on the view is from another model. For example, the logon control may display the name and email of the current user, but the main part of the view has little to do with the user. Viewdatadictionary can be used to transmit models that are not in contact, but child action can be used. The usage of child action is described here for information that is not closely related to the subject in the view. The following is an example.
The current user information is displayed on the template page. After a user logs on, the user name, email address, and other information are displayed. When the user does not log on, the login connection is provided. Add the following code to the template page:
<div id="logindisplay"> <%= Html.Action("LogOnWidget", "Account") %> </div>
The code for logonwidget is as follows. childactiononly ensures that this method can only be called through renderaction.
[ChildActionOnly]public ViewResult LogOnWidget(){ bool isAuthenticated = Request.IsAuthenticated; Profile profile = null; if (isAuthenticated) { var username = HttpContext.User.Identity.Name; profile = _profileRepository.Find(username); if (profile == null) { profile = new Profile(username); _profileRepository.Add(profile); } } return View(new LogOnWidgetModel(isAuthenticated, profile));}
Use a user control to display the model information of this action. The user control code is as follows.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %><%@ Import Namespace="AccountProfile.Controllers"%><% if (Request.IsAuthenticated) {%> Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>! [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> | <%= Html.ActionLink("Profile", "Show", "Profile", new RouteValueDictionary(new { username = Html.Encode(Page.User.Identity.Name) }), null)%> ]<% } else {%> [ <%= Html.ActionLink("Log On", "LogOn", "Account") %> ]<% }%>
Summary:This article describes three methods to refactor your view code. Using these three methods can greatly reduce repeated view-layer code. Since I recently started to learn Asp.net MVC, if you have any questions about the description and understanding, please criticize and correct it.
Refer:Asp.net MVC2 in action