Use ASP. NET MVC3 + EF + Jquery to create a text live broadcast system (3)

Source: Internet
Author: User

In the previous article, the login function was simply implemented. In this article, the background framework was first merged.

Build a framework

Or use the template mentioned above, which uses the frameset framework. net mvc, how to use the frameset framework, I recommend you read an article in ASP. net mvc uses the frameset framework !.

In the previous article, we created a new Admin folder under the View folder. Therefore, we should first create a new AdminController and add the following code:

public class AdminController : Controller{    //    // GET: /Admin/    [Authorize]    public ActionResult Index()    {        return View("Index");    }    [Authorize]    public ActionResult Top()    {        return View("Top");    }    [Authorize]    public ActionResult Left()    {        return View("Left");    }}

Create the following view:

In Index. cshtml, use frameset to reference Top. cshtml and Left. cshtml. For details, refer to the article above.

<frame src="@Url.Action("Top")" noresize="noresize" frameborder="NO" name="topFrame" scrolling="no" marginwidth="0" marginheight="0" target="main" />

Modify the LogOn action in AccountController as follows:

/// POST:/Account/LogOn [HttpPost] public ActionResult LogOn (LogOnViewModel model) {if (ModelState. isValid) {if (context. users. any (u => u. userName = model. userName & u. password = model. password) {FormsAuthentication. setAuthCookie (model. userName, false); // return RedirectToAction ("Index", "Admin");} else {ModelState. addModelError ("", "incorrect user name or password") ;}} return View (model );}

In this way, the framework is basically ready, and the effect is as follows:

Implement User Management

To implement user management, we first modify LiveText. the User entity in the Domain project, because we use the database generated by Code-First, if the entity is modified, an error will occur again during the next operation, so the First thing to do is in LiveText. create a LiveTextDbInitializer class in the Model folder of the WebUI project to regenerate the database when the object changes.

public class LiveTextDbInitializer : DropCreateDatabaseIfModelChanges<LiveTextDbContext>{    protected override void Seed(LiveTextDbContext context)    {        context.Users.Add(new Domain.Entities.User        {            UserName = "admin",            Password = "admin"        });        base.Seed(context);    }}

Register it in the Application_Start method in Global. asax:

Database.SetInitializer(new LiveTextDbInitializer());

Now we can modify the User entity. If you have written them at the beginning, you don't need to modify them now ,. The modified code is as follows:

Public class User {// <summary> // User ID /// </summary> public virtual int UserID {get; set ;} /// <summary> /// username /// </summary> [Required (ErrorMessage = "cannot be blank")] [StringLength (30)] [Display (Name = "UserName")] public virtual string UserName {get; set ;} /// <summary> /// User Password /// </summary> [Required (ErrorMessage = "cannot be blank")] [DataType (DataType. password)] [StringLength (30, MinimumLength = 5, ErrorMessage = "The minimum Password is 5 characters")] [Display (Name = "Password")] public virtual string Password {get; set ;}}

Next, create a UserController and select it as shown in.

Finally, modify the hyperlink in Left. cshtml:

<Li> <a href = "@ Url. action ("Create", "User") "target =" main "> Add a User </a> </li> <a href =" @ Url. action ("Index", "User") "target =" main "> User List </a> </li>

OK, run the program again:

No style, so it's hard to see

Implement text live Management

First, implement category management.

Modify the Category object.

Public class Category {// <summary> // Category ID // </summary> public virtual int CategoryID {get; set ;} /// <summary> /// category name /// </summary> [Required (ErrorMessage = "cannot be blank")] [Display (Name = "category Name")] public virtual string Name {get; set ;} /// <summary >/// Title set /// </summary> public virtual List <Title> Titles {get; set ;}}

Create a CategoryController.

Now, the category management is complete. Let's take a look at the running effect:

Second, achieve Title Management

First, modify the Title object.

Public class Title {// <summary> /// Title number /// </summary> public virtual int TitleID {get; set ;} /// <summary> /// category ID /// </summary> [Required (ErrorMessage = "category cannot be blank")] public virtual int CategoryID {get; set ;}//< summary> /// title name /// </summary> [Required (ErrorMessage = "title cannot be blank")] [Display (Name = "title Name")] public virtual string Name {get; set ;} /// <summary> /// Category /// </summary> public virtual Category {get; set ;} /// <summary> /// Text set /// </summary> public virtual List <Text> Texts {get; set ;}}

Create TitleController.

Now, let's take a look at the running effect:

Implement Text Management

First, modify the Text object.

Public class Text {// <summary> // Text number /// </summary> public virtual int TextID {get; set ;} /// <summary> /// title no. /// </summary> [Required (ErrorMessage = "title cannot be blank")] [Display (Name = "title")] public virtual int TitleID {get; set ;} /// <summary> /// publisher /// </summary> [Required (ErrorMessage = "publisher cannot be blank")] [Display (Name = "publisher")] public virtual string Prolocutor {get; set ;} /// <summary> /// post content /// </summary> [Required (ErrorMessage = "post content cannot be blank")] [Display (Name = "posted content")] public virtual string ProContent {get; set ;} /// <summary> /// title /// </summary> [Display (Name = "posting time")] public virtual DateTime ProDate {get; set ;} /// <summary> /// Title // </summary> [Display (Name = "Title")] public virtual Title {get; set ;}}

Create TextController.

OK. Check the running result:

So far today, JQuery will be used in the next article.

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.