ASP. net mvc 4.0 learning 6-Model Binding, mvc6-model

Source: Internet
Author: User

ASP. net mvc 4.0 learning 6-Model Binding, mvc6-model

I,ViewData, ViewBagAndTempData

ASP. in the net mvc Architecture, access is performed through the ViewData, ViewBag, TempData, and View plane of the Controller, combined with a small amount of data.

1.1 ViewBag

ViewBag can generate dynamic upload. We create a new preview to see how to use ViewBag:

Region value in Controller: ViewBag. Title = "First Region" View region value @ ViewBag. Title

1.2 ViewData

Value of region in Controller: ViewData ["message"] = "This is ViewData Value ";

Value in View Details: @ ViewData ["message"]

1.3 TempData

Unlike ViewBag and ViewData, TempData is used to store data in sessions,

Its lifecycle exists in response to the entire Request. It can be used between the Controller and the Controller for storing data.

Public ActionResult Index () {// ViewData ["ViewDataValue"] = "This is ViewData Value"; // TempData ["TempDataValue"] = "This is TempData Value "; // ViewBag. message = "modify this notebook to start your ASP. net mvc application. "; Return View ();}View Code II, Model failover (Model Binding)

In ASP. net mvc, Model Binding is used to obtain the information in the View from the Controller component.

2.1Simple Model failover

In the following example, the View plane has a text box with Content id, and the corresponding Action has a response Content with the same name, in this case, when the Action is being processed, the program will upload the information from the View plane to the Action with the same name through defamodelmodelbinder.

View:

@ Using (Html. beginForm () {<div> @ Html. label ("Please refer to Content content Content:") <input type = "text"/name = "content"> </div> <div> @ Html. label ("your Ingress content:") @ ViewData ["content"] </div> <input type = "submit" value = "submit"/>}

Action:

        public ActionResult TestAction(string content)        {            ViewData["Content"] = content;            return View();        }

Let's take a look at it next. After the click button is submitted, the value in the content text box will be converted to the TestAction value through the data transmission model of the ticket, then retrieve the value through ViewData ["Content.

2.2 FormCollection

ASP. net mvc not only obtains the View metadata through a simple model, but also obtains the entire client metadata through FormCollection.

Add the FormCollection dataset to the Action to obtain the table metadata.

View:

@ {ViewBag. title = "TestAction" ;}< h2> TestAction Action:

// FormCollection public ActionResult TestAction (FormCollection form) {ViewData ["Content"] = form ["content"]; return View ();}View Code

2.3Linearregression Model

1. We have added TestFormModel categories, which define three features.

Using System; using System. collections. generic; using System. linq; using System. web; using System. componentModel. dataAnnotations; namespace MvcApplication3.Models {public class TestFormModel {public string Content {get; set;} public string UserID {get; set;} public int Age {get; set ;}}}View Code

2. Change the value of Action sheet to TestFormModel to receive the Form table generated by the lateral View,

As long as there is a one-to-one response between the operator bit names of the Form table orders and the semantics of the Model class, the receiving operation can be completed.

View:

@ {ViewBag. title = "TestForm" ;}< h2> TestForm Action, remember to add Reference Model

// The Zookeeper model is connected to the public ActionResult TestForm (TestFormModel form) {ViewData ["Content"] = form. content; ViewData ["UserID"] = form. userID; return View ();}View Code

3. The value of the View data plane passed through the Form after being transferred to the Controller in the format of Model type, passed through the ViewData plane.

We can see the data statistics below:

2.4Result of determining the signature model

In the previous example, we can see a one-to-one correspondence between Form table single data in the View and Model, in this way, we can put the data verification part in the Model for processing.

When the processing model is disconnected in the Controller, the program will automatically handle the work of the model certificate, and the result of the certificate will be stored in the ModelState object.

Now we have updated the atomicity in the Model. Adding [Required] above indicates that this atomicity must have a value when ModelState. IsValid = true

 

Model:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;namespace MvcApplication3.Models{    public class TestFormModel    {        [Required]        public string Content { get; set; }        [Required]        public string UserID { get; set; }        [Required]        public int Age { get; set; }    }}

View:

@ {ViewBag. title = "TestForm" ;}< h2> TestForm 

Action

/// Model verification public ActionResult TestForm (TestFormModel form) {if (ModelState. isValid) {// the data in the Model conforms to the standard ViewData ["Message"] = "Verification passed"; ViewData ["Content"] = form. content; ViewData ["UserID"] = form. userID;} else {ViewData ["Message"] = "invalid certificate missing";} return View ();}

 


How does aspnet mvc40 make a page not contain template page elements, without affecting other pages?

Are you talking about the master page?

How to further learn about aspnet (MVC)

You are a newbie to learn. NET
Learn MVC again
If you are a newbie to learn. NET and a newbie to learn mvc, it may be different.
If you want to learn. Net to the framework and design mode stage
We recommend that you learn MVC
If not. Net

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.