ASP. net mvc 4 Practice Study Notes 4: controller (on ),

Source: Internet
Author: User

ASP. net mvc 4 Practice Study Notes 4: controller (on ),

I had a two-day weekend carnival. I had a lot of work tasks this week and did not study for a few days...

I. Controller and action:

From the preceding content, we can see that the controller action has two features: (1) public modification; (2) Returning ActionResult.

However, the action does not have to return ActionResult. It can be void or another action, for example, the following two examples:

 1 public class SimpleController:Controller 2 { 3      public void Index() 4     { 5         Response.Write("

It should be noted that ControllerActionInvoker (Controller action caller) always encapsulates the return value of an action into an ActionResult.

In addition, we can use NoActionAttribute to modify the public method to prevent the public method on the controller from being an action:

1 public calss SimpleController:Controller2 {3     [NoAction]4     public string SomePublicMethod()5     {6         return "Hello Wordl !";7     }8 }

2. Controller tasks:

The Controller has two common tasks: manually ing the view model and accepting user input (performing verification ).

1. Manually map the view model:

A view model is a model object created separately to display data on the screen. As application complexity increases, the view data and model structure are often different. For example, a page of the Guestbook application shows the number of comments for each user:

1) Add a view model:

1 namespace Guestbook.Models2 {3     public class CommentSummary4     {5         public string UserName { get; set; }6         public int NumberOfComments { get; set; }7     }8 }

2) Add the Controller action:

 1 public ActionResult CommentSumary() 2         { 3             var entries = from entry in db.Entries 4                           group entry by entry.Name 5                               into groupByName 6                               orderby groupByName.Count() descending 7                               select new CommentSummary 8                               { 9                                   NumberOfComments=groupByName.Count(),10                                   UserName=groupByName.Key11                               };12             return View(entries.ToList());13         }

3) Add a view:

1 @ model IEnumerable <Guestbook. models. commentSummary> 2 3 @ {4 ViewBag. title = "CommentSumary"; 5} 6 7 2. input verification:

In the previous Create action of GuestbookController, user input is accepted and no verification is performed. The following improvements are made:

1) Add verification annotation properties -- modify GuestbookEntry:

 

 1 namespace Guestbook.Models 2 { 3     public class GuestbookEntry 4     { 5         public int Id { get; set; } 6         [Required] 7         public string Name { get; set; } 8         [Required] 9         public string Message { get; set; }10         public DateTime DateAdded { get; set; }11     }12 }

 

2) check whether the verification is successful -- modify the create action:

1 [HttpPost] 2 [ValidateAntiForgeryToken] 3 public ActionResult Create ([Bind (Include = "Id, Name, Message, DateAdded")] GuestbookEntry guestbookentry) 4 {5 if (ModelState. isValid) // check whether the verification is successful 6 {7 db. entries. add (guestbookentry); 8 db. saveChanges (); 9 return RedirectToAction ("Index"); 10} 11 12 return View (guestbookentry); // re-render the form when a failure occurs 13}

3) display the error message in the View:

@Html.ValidationSummary(true)

Note: To display custom messages in the view, you only need to modify the declaration of the Required annotation attribute, for example:

[Required (ErrorMessage = "Enter your username")] public string Name {get; set ;}

If you do not want to hard-encode the message, you can specify the name and type of the source file:

[Required (ErrorMessageResourceType = typeof (type), ErrorMessageResourceName = Name]

I don't know if something is wrong with the Internet or the blog garden. I lost some of the content, and I will try again later...

Related Article

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.