Strongly typed HTML template method and direct display model

Source: Internet
Author: User

Model:

public class article
{

[Display (name= "article")]
public string ArticleID {get; set;}

[Display (name= "category")]
public string CategoryID {get; set;}

[Display (name= "title")]
[Required]
[Stringlength (50)]
public string Title {get; set;}

[Display (name= "body")]
[DataType (Datatype.multilinetext)]
public string Content {get; set;}

[Display (Name = "Author")]
[Stringlength (20)]
public string AuthorName {get; set;}


[Display (Name = "date published")]
[DataType (Datatype.date)]
[DisplayFormat (dataformatstring= "{0:yyyy-mm-dd}", Applyformatineditmode=true)] only fit in view Html.dipspalyfor (a =>a.posttime) @Html. Edittorfor (a =>a.posttime)

Or the model uses @Html when traversing the ienumrable model for ienumrable<articles>. Dipspalyfor (ModelItem =>a.posttime) to truly reference the countless model,  appears as a date format. If you only use article in the view. Posttime does not reference data annotations, or displays time
[DisplayFormat (dataformatstring = "{0:yyyy-mm-dd HH:mm:ss}", Applyformatineditmode = True)]
[DataType (Datatype.datetime)]
[DisplayFormat (dataformatstring = "{0:yyyy-mm-dd HH:mm:ss}", Applyformatineditmode = True)]
Public DateTime posttime {get; set;}

[Display (name= "priority")]
[Range (1,100,errormessage= "{0} must be between {1} to {2}")]
public int Priororder {get; set;}


[Display (name= "traffic")]
public int Clickcount {get; set;}


Public virtual category category {get; set;}

Public article ()
{
This. ArticleID = Guid.NewGuid (). ToString ();
}

Controller:

Public ActionResult List (string categoryid,int? page)
{
if (string. IsNullOrEmpty (CategoryID))
return new Httpnotfoundresult ();

Viewbag.categoryname = _categoryservice.find (c =>c.categoryid = = CategoryID). CategoryName;
Viewbag.categoryid = CategoryID;

int pagenumber = page?? 1;
Viewbag.page = pagenumber;
int recordperpage = 20; Sets the number of records per page.
Viewbag.recordperpage = Recordperpage;

int totalrecord = 0; Defines an output parameter. In fact, the output parameter is assigned a value of 0, is not much significance. Just to be able to access this variable name. The output parameter, like the reference parameter, occupies the same memory space as the parameters, and the parameter values change, and the values of the output parameters are changed. The output parameter is to solve a problem where a function in C # can only return a single value, and use it to return multiple values.

var articlelist = _articleservice.findpagelisttwoorder<int,datetime> (PageNumber, RecordPerPage, out Totalrecord, a = A.categoryid = = Categoryid,ordertype.asc,a =>a.priororder,ordertype.desc, a = A.PostTime);
var artilelist = _articleservice.findall (). Where (A = = A.categoryid = = CategoryID). (a =>a.priororder). OrderByDescending (a =>a.posttime);

Viewbag.totalrecord = Totalrecord;
Viewbag.totalpage = (int) math.ceiling (double) Totalrecord/(double) recordperpage); Math.celling () takes the whole function up. Similarly, Math.floor () takes the entire function down.


Return View (Articlelist.tolist ());
}

View:

@model ienumerable<majorconstruction.models.article>

@{
Viewbag.title = "List";
}


<!--breadcrumbs Navigation--

@*<ol class= "breadcrumb" >
Your current location:
<li><a href= "@Url. Action (" Index "," Home ")" ><span class= "Glyphicon glyphicon-home" > Home </span ></a></li>
<li class= "Active" ><span class= "Glyphicon glyphicon-th-large" > @ViewBag. categoryname</span></ Li>
</ol>
*@

<div class= "Page-header" >

<div class= "Row" >
<div class= "Col-md-3" >

@{html.renderaction ("Leftsidemenu", "Home", new {area = "", Activecategoryid = Viewbag.categoryid});} <!--//Returns a navigation menu panel that passes a number of arguments through the linked RouteValue. -

@{html.renderaction ("Clickrangeoncategory", "Home", new {area = "", Activecategoryid = Viewbag.categoryid});} <!--//Return a click on this column--


</div>


<div class= "Col-md-9" >
<table class= "Table Table-hover table-striped" >
@foreach (Var article in Model)
{
<tr>
<td>
<a href= "@Url. Action (" Showarticle "," article ", new {ArticleID = article. ArticleID, area = "}") "><span class=" Glyphicon glyphicon-file "> @article. Title</span></a>
</td>
<td>
@Html. Displayfor (ModelItem =>article. Posttime) //strongly typed auxiliary methods refer to the data annotations of the model class and are displayed in a date format. If @article is used at this time. Posttime instead of using displayfor strongly typed helper methods, the DateTime format is displayed. Instead of just a date format.
</td>
</tr>


}
<tfoot>
<tr>
<TD class= "text-muted" colspan= "3" >
Each page @ViewBag. Recordperpage records, Total @ViewBag. Totalrecord Records. The @ (Viewbag.totalrecord = = 0? 0:viewbag.page) page, total @ViewBag. totalpage page @* If the number of records queried is 0, it is displayed as page No. 0. Here is a conditional expression that is intended to avoid a total of 0 pages, as in page 1th. The situation. *@
</td>

</tr>
</tfoot>
</table>


@if (Viewbag.totalrecord! = 0)//is to avoid having no records, or to display the next page of symbolic links. This will cause a bug in the program, can be a point of the next page of the symbol, but there is no record.
{
<ul class= "Pagination" >
@if (Viewbag.page! = 1)//If the current page is not page 1th, a symbolic link is displayed << previous page. The current page is page 1th and the << is not displayed.
{
<li><a href= "@Url. Action (" List ", new {Categoryid=viewbag.categoryid, page = (int) (viewbag.page)-1})" >& Amp;laquo;</a></li> @* to ensure consistency between paging and filtering, the routing parameters are added to the link. and pass the current value back to each input form field via ViewBag. *@
}

@for (int page = 1; page <= (int) @ViewBag. totalpage; page++)
{
String activecss = page = = (int) viewbag.page? "Active": null;
<li class= "@activeCss" ><a href= "@Url. Action (" List ", new {CategoryID = Viewbag.categoryid, page = page})" >@p Age</a></li>

}
@if (viewbag.page! = viewbag.totalpage)//If the current page is not the last page, display the >> next page of the symbolic link. The current page is the last page and the >> is not displayed.
{
<li><a href= "@Url. Action (" List ", new {CategoryID = Viewbag.categoryid, page = (int) (viewbag.page) + 1})" >&am P;raquo;</a></li>
}

</ul>
}

</div>
</div>

Strongly typed HTML template method and direct display model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.