Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add, delete, change and check MVC

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to add, delete, change and check MVC". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "how to add, delete, change and check MVC".

First of all, create an application for MVC2, in which we have created the controller folder Controller, template Models, and Views view

1. Let's create a diagram of the steps in the controller.

You have to change your name here, but don't change the following Controller. This is Microsoft's agreement.

Right-click over the Index and click this to add a view

It is best to change this name to your controller name, select this to create a strongly typed view, select the database you want in the view data class, and select the view content as List, because the content is to be displayed.

two。 We need to display all the information of this user on the page.

We take the entity object context in this Models to the

3. Show the user's information.

Public ActionResult Index () {/ / display all the data, of course, here is still paging ha EFFristModelEntities ef = new EFFristModelEntities (); / / get the context return View (ef.UserInfo); / / this method has multiple overloads}

Here is the user information we are going to show.

4. Let's display a piece of data fetched from the user, and create a view in this way for both additions, deletions, changes and queries.

5. First of all, take out a piece of user information to display, delete and edit all the user information that is being deleted and edited, and first display the user information currently clicked on. It was found according to the user Id

Public ActionResult Index () / / all the user information is displayed here

{

/ / display all the data, of course, it still has to be paginated here.

EFFristModelEntities ef = new EFFristModelEntities (); / / get the context

Return View (ef.UserInfo); / / this method has multiple overloads

}

/ /

/ / GET: / Student/Details/5

/ / /

/ / data page display is based on this ID to display a certain piece of data.

/ / /

/ / /

/ / /

Public ActionResult Details (int id)

{

/ / get the context

EFFristModelEntities ef = new EFFristModelEntities ()

/ / use this context to get the user's information using Where

/ / Where (uId = > uId.ID = = id): is a collection FirstOrDefault (): a piece of data

Var userInfo= ef.UserInfo.Where (u = > u.ID = = id) .FirstOrDefault ()

ViewData.Model = userInfo;// fetches the entity with this ViewData, and then gives the entity object userInfo to ViewData, because this is a strongly typed page that directly uses Model to fetch the foreground Inherits= "System.Web.Mvc.ViewPage".

Return View ()

}

/ /

/ / GET: / Student/Create

/ / /

/ / add user information

/ / /

/ / /

To add a user is to register in the same way.

/ /

/ / GET: / Student/Edit/5

/ / /

/ Editing user's information according to id

/ / /

/ / /

/ / /

Public ActionResult Edit (int id)

{

/ / get the context

EFFristModelEntities ef = new EFFristModelEntities ()

Var user = ef.UserInfo.Where (u = > u.ID = = id) .FirstOrDefault

ViewData.Model = user

Return View ()

}

/ /

/ / POST: / Student/Edit/5

/ / /

/ Editing user's information according to id

/ / /

/ / /

/ / /

/ / /

[HttpPost]

Public ActionResult Edit (int id, UserInfo user)

{

Try

{

EFFristModelEntities ef = new EFFristModelEntities ()

Ef.UserInfo.Attach (user); / / append this user to this context

Ef.ObjectStateManager.ChangeObjectState (user, System.Data.EntityState.Modified); / / changed the status of this user.

Ef.SaveChanges ()

Return RedirectToAction ("Index")

}

Catch

{

Return View ()

}

}

/ /

/ / GET: / Student/Delete/5

/ / /

/ Delete data according to id

/ / /

/ / /

/ / /

Public ActionResult Delete (int id)

{

EFFristModelEntities ef = new EFFristModelEntities ()

Var userInfo=ef.UserInfo.Where (u = > u.ID = = id) .FirstOrDefault ()

ViewData.Model = userInfo

Return View ()

}

/ /

/ / POST: / Student/Delete/5

/ / /

/ / this deletion is submitted by post. You need to add an attribute tag [HttpPost] before it.

/ / /

/ / /

/ / /

/ / /

[HttpPost]

Public ActionResult Delete (int id, FormCollection collection)

{

Try

{

EFFristModelEntities ef = new EFFristModelEntities (); / / find the context

/ / find Id

Var user=ef.UserInfo.Where (u = > u.ID = = id) .FirstOrDefault ()

/ / determine whether the user is null

If (user! = null)

{

Ef.UserInfo.DeleteObject (user); / / use this context to fetch the user id and delete it

Ef.SaveChanges ()

Return RedirectToAction ("Index"); / / if the deletion is successful, go to the page that displays the data.

}

Return Content (No data)

}

Catch

{

Return View ()

}

}

The above is all the contents of this article "how to add, delete, change and check MVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report