ASP. net mvc series: controller Edit Method, mvcedit

Source: Internet
Author: User

ASP. net mvc series: controller Edit Method, mvcedit

We have introduced how to create controllers, views, and data models, run previous projects, open the Movies homepage, and hover your mouse over the edit menu, you will see the address of the "edit" link in the lower right corner of the browser.

First, check the view code and find the code corresponding to the "edit" item.

The Code prompts us to know that the ActionLink method returns the positioning point element (element a) containing the virtual path of the specified operation. The HTML auxiliary method simplifies the view encoding, all model values are HTML encoded by these auxiliary methods before rendering. In the browser, we can view the auxiliary methods to generate the corresponding HTML code.

 

Return to the address you saw in the lower-right corner of the browser. It uses the RouteConfig class (you canApp_StartFolder) generated. ASP. NET converts the URL request address to the corresponding call method.

    public class RouteConfig    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }            );        }    }

In, you can directly submit http: // localhost: 51894/movies/Edit? Id = 2, then the page will go to the editing page

Now let's take a look at what operations are done by the Edit Method in the controller.

In the code, we can see two Edit methods. One of them is marked with the [HttpPost] attribute, and the other is not. In fact, the system has provided the [HttpGet] attribute by default, we can also see from the generated code comments. In general, Get requests are used to read data, while Post requests are used to write data. Post requests usually change the status on the server, repeated submission of Post requests may produce duplicate data, but the Get request will not change the server status. Therefore, repeated sending of Get requests to the server will not cause any impact; for more get and post information, see the difference between GET and POST.

From the Edit Method of the Get request, we can see that it uses the passed parameter (id) to find the corresponding data from the database context. If there is data, it will be returned to the view, when creating a view, use @ model MvcMovie. models. movie declares the specified type of the view and generates a view template of this type. If no data is found, it directly returns an HttpNotFoundResult object through the HttpNotFound method.

For the Edit Method of the Post request, it receives an object parameter (User-updated data) and then verifies whether the data can be updated to the database, finally, the SaveChanges method in the data context is used to update the data in the database.

db.Entry(movie).State = EntityState.Modified;

If the data passes the verification, you can use this line of code to inform the data context that the object already exists in the Database. Therefore, you do not need to create a new data record, but update the old data.

  http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

  

 

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.