MVC5 + EF6 complete tutorial 2, mvc5ef6 getting started tutorial

Source: Internet
Author: User

MVC5 + EF6 complete tutorial 2, mvc5ef6 getting started tutorial

Start with the front-end UI

MVC separation is better, and there is no special requirement on the development sequence. This time we will mainly explain the front-end UI.

ASP. net mvc abandons some of the unique habits of WebForm, such as server controls and ViewState, so that the Web can return to the original state and what the Web looks like. Speaking of a Web Application UI involves nothing more than html, css, and js.

Outline
  • Overview
  • Important Concepts
  • Create registration/logon UI step (Static Page à View à function)
Overview

Generally, two functions are required for each system.

One is user registration/login, and the other is Table CRUD

Planning of the last four articles:

P.S. By the way, I would like to answer two questions from yuanyou:

1. Some garden friends said they did not see EF

The reason is that I don't want to introduce too many concepts. EF is involved in the next article.

2. Number of articles and updates in the series:

A. Quantity: this series of articles will begin from a backbone, gradually deepen, and initially plan 30 articles. 10 For Beginners, 10 for intermediate users, and 10 for comprehensive projects.

B. Update: A new article will be updated every week.

Important Concepts

Apply it to the Action of the Controller to help select the appropriate Action,

[HttpPost] is used as an example.

One way to transmit data in View and Controller (similar methods include ViewData and TempData), master to transmit data in View and Controller through ViewBag

This function is called by using the Html attribute of View. The following uses Html. BeginForm as an example.

 

Procedure

Knowledge points and Demo code of this series of articles are based on the previous article. If you have any questions, you can go back to the previous article for search. This time, we will create two pages for user registration/login.

Open Controllers à AccountController. cs and add two actions, as shown in figure.

NOTE: You can add this Action.

A. Copy by hand or by pasting,

B. Right-click and insert the code segment (or ctrl k, x) à ASP. NET MVC4 à mvcaction4 (or mvcpostaction4), as shown in the following figure.

According to the Action added in the previous step, add the related View: Login. cshtml, Register. cshtml

For more information about adding methods, see the previous article.

The storage location conventions of the View are described here. Remember the following three sentences:

1.1 we regard Login. cshtml as a static html page to complete the UI of the logon interface.

You can understand cshtml as a mixture of the original aspx and html:

Makes use of the advantages of aspx to facilitate interaction with the background; makes use of the advantages of html, the syntax is concise (HtmlHelper)

A. Copy the logon page html on bootstrap.

Http://getbootstrap.com/css/#forms

B. Put it in the div of the body of Login. cshtml.

NOTE: To reduce interference items, we do not add any styles. After the project is all completed, page beautification is performed.

Right-click the Browser View page source and find that the VS Browser Link waste code is missing.

We disable it.

Open web. config and add the following code. Then you can view the source code in the browser to see the pure html.

Complete a logon:

Enter the form à Controller to obtain the form data. For example, you can go to the database for comparison and then retrieve the user identity to go to the specified page)

This time, we will first obtain data from the Controller. The next chapter of database operations will be combined with EF.

OK. Now we can modify Login. cshtml.

Login. cshtml before modification:

Note that a [HttpPost] is added to the newly added Action, indicating that this Action only accepts http post requests. ASP. net mvc provides Action Method Selector, and HttpPost is one of them. (The Selector will be explained in a later article)

Typical application scenarios of HttpPost attributes:

When it comes to receiving client window data, create an Action for receiving HTTP Get requests to display the interface and provide the user with data;

Another Action with the same name applies the [HttpPost] attribute, which is used to receive user data and complete the corresponding function.

Add two attributes, action and method, to the form tag. For methods in form (the default value is get), get is usually used for simple Data Reading operations, and post is used for Data Writing operations.

Add the name attribute under the input element and set the same value as the id.

NOTE: The server must be named by name.

2. Open AccountController. cs and modify the Login Action of [HttpPost] to receive data.

To distinguish between the two sides, we pass a logon status through ViewBag.

A text mark is also added to the front-end.

Logon result:

 

Optimization: Use the first HtmlHelper

Because this time the content is relatively simple, there is no need to use HtmlHelper

We have made some improvements to the form in Login. cshtml.

For example, the location of the action is fixed. If the deployment changes, the address may be unavailable (for example, it is different to the virtual directory in the IIS root directory)

Using HtmlHelper to dynamically calculate the route address is one of the methods.

Add the following code and put the content in form in {}.

@ Using (Html. BeginForm ("login", "Account", FormMethod. Post )){}

Run and view the source code in the browser. The generated source code is the same as the original one.

 

Similarly, complete the registration Interface UI (similar to the logon interface, steps are omitted)

Summary

A basic logon interface is developed to show you how to obtain form data from the Controller.

Because this example is relatively simple, HtmlHelper is not required

The next article will automatically generate a database using the model to complete the entire function.

This source: http://files.cnblogs.com/miro/MVCDemo%401021.rar

 

If you have any questions, please comment :)


After upgrading EntityFramework500 to EntityFramework600, the following error occurs. How can this problem be solved?

Point: MVC5 + EF6 or MVC4 + EF5
 
360 browser: 0*01c94ef6 command reference "0*00000008 memory this memory cannot be considered" read

If you are using a Windows operating system, you may encounter the following error message:
「 "0X ????????" The memory of the instruction reference "0x00000000". The memory cannot be "read" or "written" and the application is closed.
If you ask some experts, the answer is often "Windows is so unstable" and so on. In fact, this error is not necessarily caused by Windows instability. This article briefly analyzes the general causes of such errors.
1. failed to check memory allocation by Application
When a program needs a piece of memory to store data, it needs to apply using the "function" provided by the operating system. If the memory is allocated successfully, the function will return the newly opened memory zone address to the application, and the application can use this memory address. This is "dynamic memory allocation", and the memory address is also the "cursor" in programming 」. The memory is not always used, and sometimes the memory allocation fails. When the allocation fails, the system function returns a value of 0. The returned value "0" does not indicate a newly enabled cursor. Instead, the system sends a notification to the application notifying you of an error. As an application, check whether the returned value is 0 after each memory application. If yes, it means that a fault has occurred and some measures should be taken to save the problem, this enhances the robustness of the program 」. If the application does not check this error, it considers this value as an available cursor for it according to "thinking inertia" and continues to use this memory in subsequent execution. The real 0-address memory zone stores the most important "Interrupt Descriptor Table" in computer systems and is definitely not allowed to be used by applications. In an operating system without a protection mechanism (such as DOS), writing data to this address will immediately become a machine, and in a robust operating system, such
Windows and so on, this operation will be immediately captured by the system's protection mechanism, the result is that the operating system forces the application to close the error, to prevent the error from expanding. In this case, the above "write memory" error occurs and the referenced memory address is "0x00000000 」. There are many causes of memory allocation failure, such as insufficient memory and mismatched system function versions. Therefore, this allocation failure is often seen after the operating system has been in use for a long time and has installed a variety of applications (including virus programs that inadvertently "Install ), after a large number of system parameters and system files are changed.
2. The application cited abnormal memory cursor due to its own BUG
In applications that use dynamic allocation, sometimes such a situation occurs: The program tries to read and write a piece of "should be available" memory, but somehow, the expected available cursor has expired. It may be that the program "forgot" to request distribution to the operating system, or the program may have logged out the memory at some time and "did not pay attention. The logged-out memory is reclaimed by the system and its access rights do not belong to this application. Therefore, read/write operations also trigger the system's protection mechanism, the only end of an attempt to "violate the law" is to be terminated and all resources are recycled. Laws in the computer world are much more effective and harsh than humans! Such cases all belong to the bugs of the program. You can often reproduce errors in a specific operation order. Invalid cursor may not always be 0, so the memory address in the error prompt is not set to "0x00000000", but other random numbers. If the system frequently mentions error prompts, the following suggestions may be explained.
:
1. check whether a trojan or virus exists in the system. Such programs tend to change the system irresponsibly to control the system,
As a result, the operating system is abnormal. In general, information security awareness should be strengthened, and the executable procedures with unknown sources should never be curious.
2. Update the operating system so that the operating system installer can copy the correct version of the system file and correct system parameters.
Sometimes the operating system itself also has bugs. You must install the officially released upgrade program.
3. Try the new version of the application.
Mode:
Remove virtual memory
Answer:
So far, this is positive, that is, if it does not happen again when the next cold day arrives, it means this is the main cause.
Append:
If you use
We recommend that you delete all *. PF files under the WINDOWS \ PREFETCH directory after the Ghost recovers OS because you need to make the remaining full text of win...>

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.