Learn how to use MVC4 for website 5: 5.7 show articles

Source: Internet
Author: User

I have prepared a directory for browsing the article, and now I am going to show the content of the article.

Open CommonModelController and add an Index (int id) action. First, find whether the content of the specified Id exists. If yes, the view is returned. If no error page is returned.

/// <Summary> /// display content /// </summary> /// <param name = "id"> public Model Id </param> public ActionResult Index (int id) {var _ cModel = cModelRsy. find (id); if (_ cModel = null) {Error _ e = new Error {Title = "the content does not exist", Details = "failed to Find the specified content from the database! ", Cause =" This content has been deleted. ", Solution = Server. urlEncode ("<li> Returns <a href = '" + Url. action ("Index", "Home") + "'> homepage </a>. </Li> ")}; return RedirectToAction (" ManageError "," Prompt ", _ e);} return View (_ cModel. Category. ContentView, _ cModel );}

Right-click to add a view.

@ Model Ninesky. Models. CommonModel @ {ViewBag. Title = Model. Title + "-" + Model. Category. Name; Layout = "~ /Views/Shared/_ Layout. cshtml ";} <div class =" banner ">  </div> <div class =" left "> <div class =" children "> <dl> <dt> @ Model. category. name </dt> <dd> @ Html. action ("PartialChildren", "Category", new {id = Model. categoryId}) </dd> </dl> </div> <div class = "content_cnt"> <div class = "path"> @ Html. action ("PartialPath", "Category", new {id = Model. categoryId}) >>> details </div> <div class = "content"> here is the content of the article </div>

In fact, it is copied from views/category/index. cshtml and slightly modified. The key is <div class = "content">... </Div>, in which I plan to display the content of the article. CommonModel contains only the public information of the article and has no specific content. It must be called from ArticleController.

Open ArticleController and add PartialDetail (int id, string view = "PartialDetail") action. It can be seen from the name that this is used to display the branch view. The id is the public model id and the view name. You can pass different view names as needed. The default value is "PartialDetail ".

/// <Summary> /// document content /// </summary> /// <param name = "id"> CommonModelId </param> /// <param name = "view"> view </param> public PartialViewResult PartialDetail (int id, string view = "PartialDetail") {return PartialView (view, articleRsy. findByCModelId (id ));}

Right-click to add a branch view

@ Model Ninesky. models. article <div class = "title"> @ Model. commonModel. title </div> <div class = "Info"> Source: @ Model. source Author: @ Model. author Release Date: @ Model. commonModel. releaseDate. toLongDateString () read: @ Model. commonModel. hits </div> <div class = "text"> @ Html. raw (Model. content) </div>

The title is displayed at the top of the view, followed by the basic information of the article, and the content of the article is displayed at the bottom. You must use @ Html. Raw () to display HTML content. If you directly write @ Model. Content, it will be HTML-encoded Content.

Return to CommonModel/index. chhtml and replace "here is the Article content" with @ Html. Action ("PartialDetail", "Article", new {id = Model. CommonModelId }).

The article content can be displayed here, but the Url format of the article content is "http: // localhost: 52270/CommonModel/Index/10". This format does not seem too friendly. I hope the url is as short as possible, easy to understand, and easy to remember. What should I do? Change the route.

Open App_Start/RouteConfig. cs and add a route named "Item" to the default route. The url format is "Items/{id }".

routes.MapRoute(                name: "Items",                url: "Items/{id}",                defaults: new { controller = "CommonModel", action = "Index", id = UrlParameter.Optional }                );

This url is quite pleasing to the eye. "/CommonModel/Index/10" is expressed as "/Items/10 ".

F5 browser to see the effect.

 

========================================================== ====

For the code, see MVC4 website 5: articles.

========================================================== ====

Thank you for your constant interest and help. I wish you a happy New Year and a happy family!

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.