I also released an MVC Program (2)

Source: Internet
Author: User

I also released an MVC Program (1). In the Performance Appraisal System of Asp.net MVC 3, we talked about model and basecontroller. Next we will talk about other controllers and views.

As mentioned above, the sub-controller inherits the basecontroller and directly uses the respective datacontext.

I. Check logincontroller first:

   1: public ActionResult Index()

   2: {

   3:     return View();

   4: }

   5:  

   6: [HttpPost]

   7: public ActionResult Index(BoeUser user)

   8: {

   9:    try { 

  10:     if (ModelState.IsValid)

  11:     {

  12:         IQueryable<PerfUser> loginUser;

13: If (user. isadmin! = True) // normal user login

  14:         {

  15:             loginUser = from o in DataContext.PerfUser where o.UserID == user.UserID && o.UserPwd == user.UserPwd select o;

  16:             if (loginUser != null && loginUser.Count() != 0)

  17:             {

  18:                 return RedirectToAction("Query", "Score", loginUser.First());                        

  19:             }

  20:  

  21:         }

22: else // log on as an administrator

  23:         {

  24:             loginUser = from o in DataContext.PerfUser where o.UserID == user.UserID && o.UserPwd == user.UserPwd &&o.UserAuthority==9 select o;

  25:             if (loginUser != null && loginUser.Count() != 0)

  26:             {

  27:                 return RedirectToAction("Index", "PerfManager", loginUser.First());                  

  28:             }

  29:         }

  30:  

  31:  

  32:         ModelState.AddModelError("LoginError", "The user name or password is wrong!");

  33:         return RedirectToAction("Index", "Login");

  34:     }

  35:         }

  36:     catch(Exception e)

  37:     {

  38:         throw e;            

  39:     }

  40:     return View(user);

  41:  

  42: }

The first index indicates the default logon view without any operations. Right-click the index method and choose add view to create a view that contains logon elements, as shown in

Why does logincontroller have two index methods? Isn't a function with duplicate names allowed in the controller? We can see that the second index method has a [httppost] attribute. In Asp.net mvc3, this is used to receive form submissions on the view page. The form submission in the view is automatically submitted to the Controller with the [httppost] attribute. Only one form is submitted here. If multiple forms are submitted, this method cannot be met and actionselector is required. In this case, you can see several methods to implement multiple button submission in the impermanence ASP. net mvc.

We can see that the view interface is very simple. There are two Textbox, one Administrator logs on to the checkbox, and the log on submit button. In fact, after writing the controller, the view interface can be handed over to the artist for beautification. the business logic is basically separated from the page artist, which is also one of the biggest advantages of MVC. Of course, the artist and coder must communicate with each other. It is best to have spec specifications to coordinate information consistency between the view and controller.

In the index method of login, we use LINQ to obtain the data set:

   1: loginUser = from o in DataContext.PerfUser where o.UserID == user.UserID && o.UserPwd == user.UserPwd select o;

Here, datacontext inherits from the parent class basecontroller and is used directly, which is much more convenient.

Use redirecttoaction to switch the Controller to redirect the page.

   1: return RedirectToAction("Query", "Score", loginUser.First());  

       

Ii. Next let's look at scorecontroller

   1: public ActionResult Index()

   2: {

   3:     return View();

   4: }

   5:  

   6: public ActionResult Query(PerfUser loginUser)

   7: {

   8:     if (loginUser == null)

   9:     {

  10:         ModelState.AddModelError("NoUser", "Not logged in!");

  11:         return RedirectToAction("Index", "Login");

  12:     }

  13:     else

14: {// obtain the logon user information

  15:         hasLoginIn=true;

  16:         ViewData["User"] = loginUser.UserName ?? loginUser.UserID;          

  17:         var scorelist=from p in ScoreDataContext.PerfScore where p.UserID==loginUser.UserID select p; 

  18:         return View(scorelist);

  19:     }

  20: }

The perfuser information passed through the login page is used to obtain the login user performance information, return the list, and create a view. You can use a strong type when right-clicking Add view, as shown in:

After you click Add, the query view is automatically created, that is, the desired performance score list page, which can be slightly modified. Of course, you can also manually write all the code by yourself.

   1: @foreach (var item in Model) {

   2:     <tr>

   3:         <td>

   4:             @Html.DisplayFor(modelItem => item.UserID)

   5:         </td>

   6:         <td>

   7:             @Html.DisplayFor(modelItem => item.Score)

   8:         </td>

   9:         <td>

  10:             @Html.DisplayFor(modelItem => item.PerfCreateTime)

  11:         </td>

  12:         <td>        

  13:             @Html.DisplayFor(modelItem => item.PerfModifyTime)

  14:         </td>

Statements such as foreach can now be written on the view interface, which was unimaginable in webform a few years ago. This is also the strength of the razor engine.

3. Use the mvcpager paging control on the usermanager page

Among them, the page control uses mvcpager, which is open-source in China. You can refer to the mvc3 + ef4.1 Learning Series (3) of love.net ----- sorting and selection and paging, I should not have written his details. You can just read his blog. The view is similar to the previous page and will not be detailed.

Okay. Let's talk about it here first. It's easy to look at the source code. It's a bit simple, mainly because it's not finished. Next we will start other projects ^_^.

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.