Formal learning MVC 03

1, View -> Controller data communication

1) via url query string

        public ActionResult Index(string user)
        {

            return Content(user);
        }

 

2) is transmitted by way of post

 

 1     ViewBag.Title = "ShowForm";
 2 }
 3 
 4 <h2>ShowForm</h2>
 5 
 6 <p>your form data will be deliver to Index</p>
 7 
 8 <form action="/Demo/Index" method="post">
 9     <input type="text" name="user" value="" />
10     <input type="submit" name="" value="提交" />
11 </form>

 

        public ActionResult Index(string user)
        {

            return Content(user);
        }

 

 

 1         public ActionResult Index(string user)
 2         {
 3 
 4             return Content(user);
 5         }
 6         public ActionResult ShowForm(string user)
 7         {
 8 
 9             return View();
10         }

 

If you need to specify the request method, you may be specified by setting the attribute

        [HttpGet]
        public ActionResult Index(string user)
        {

            return Content(user);
        }

We will not respond to a request post

Do not recommend a list of parameters, required parameters directly integrated into a class as an argument

       public ActionResult Index(Models.Student model)
        {

            // code

        }

Recommended reason there are so written can set the Model Properties

Need to reference:

using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations;

namespace MVCStudy.Models
{
    public class Student
    {
        [Required]
        public int Id { get; set; }
        [Required,StringLength(maximumLength:12,MinimumLength = 2)]
        public string Name { get; set; }

    }
}

And checking in the controller

        public ActionResult Index(Models.Student model)
        {
            if (!ModelState.IsValid)
            {
                return Content("your data is not right");
            }
            return Content(model.Name);
        }

If you want to be prompted view page. The method may be appropriate in view of the new right-Controller:

Set model class, will automatically generate a view of the front end of the check (strongly typed view)

At this writing, respectively controller get and post requests:

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        // GET: Demo
        [HttpPost]
        public ActionResult Index(Models.Student model)
        {
            if (!ModelState.IsValid)
            {
                return Content("your data is not right");
            }
            return Content(model.Name);
        }

 

2, the automatic generation of views explaining a page

1) Begin Form

Similar to the form tag, the default page for the current Action

@model MVCStudy.Models.Animal

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Animal</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

BeginForm method parameters

actionName method name

controllerName controller name corresponding to the above-described action

routeValues ​​route values

FormMethod way when submitting the request

HtmlAttribute html tag attributes within

 

Label EditorFor

LabelFor modify the text can be modified within the Model

A display mode setting data (the DataType)

    public class Animal
    {
        [Required]
      [DataType(DataType.Password)] [Display (the Name
= " Animal Name " )] public String the Name { GET ; SET ;} [Required] [Display(Name = "性别")] public string Sex { get; set; } }

[The EmailAddress] check mailbox

Custom fields input error message: [EmailAddress (ErrorMessage = "XXXXX")] ()

Log in general use create templates

 

②AntiForgeryToken

To prevent the page being transformed, expressed as a Inpuit of hidden fields,

Page can not be transformed or submit fake data, but also to write the controller in use

 

       [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Models.Animal model)
        {
            if (!ModelState.IsValid)
            {
                return Content("your data is not right");
            }
            return Content(model.Name);
        }

 

③ ValidationSummary

Presents an error message:

Set in the Controller

       [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Models.Animal model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(key:"",errorMessage:"wrong data")
            }
            return Content(model.Name);
        }

 Controller and returns the view provided AddModelError

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Models.Animal model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(key: "", errorMessage: "wrong data");
            }else if (model.Name == "Desk")
            {
                ModelState.AddModelError (Key: "" , errorMessage: " This is the table are not animals " );
                 return View (Model);
            }
            return Content(model.Name);
        }

Results are as follows:

The red text is ValidationSummary

A plurality of display fields accumulated errors

 

⑤htmlAttributes

Settings tab of the property

htmlAttributes:new{}

 General form page two requests are get and post mode

get used to display the form pages

Post processing requests submitted, and interactive databases, etc.

 

Guess you like

Origin www.cnblogs.com/Tanqurey/p/12316118.html