View-control-model base, strongly typed view, add validation Sample

Source: Internet
Author: User
Tags form post

Views\home\index.cshtml

@* assigns the data of the form post to the properties of the parameter object through the binder mechanism *@
@model MvcHelloWorld.Models.Person

@{
Viewbag.title = "Index";
}

<div>
@using (Html.BeginForm ("Sendinformation", "Home"))
{
<p> @Html. ValidationSummary () </p>
<p>your Name: @Html. Textboxfor (x=>x.name) </p>
<p>your Email: @Html. Textboxfor (X=>x.email) </p>
<p>your Phone: @Html. Textboxfor (X=>x.phone) </p>
<p>gender:
@Html. Dropdownlistfor (X=>x.gender,
new[]{
New SelectListItem () {text= "I am Boy", selected=true,value= "Male"},
New SelectListItem () {text= "I am Girl", selected=false,value= "Female"},
}, "Please select your gender") </p>
<p><input type= "Submit" value= "Submit Your Information"/></p>
}
</div>

Views\home\welcome.cshtml

@{
That displays the title of the current page.
Viewbag.title = "Welcome";
}

Models\homemodels.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;

Verify that the namespace must be applied, add @html.validationsummary () to the page, and add in action
if (modelstate.isvalid)
//{
Return Content ("Thank," + person.) Name + "," + person. Email + "," + person. Phone + "," + person. Gender);
//}
Else
//{
Return View ("Index");
//}
Using System.ComponentModel.DataAnnotations;

Namespace Mvchelloworld.models
{
public class Person
{
[Required (errormessage= "Please enter your name")]
public string Name {get; set;}

[Required (errormessage= "Please enter your email address")]
public string Email {get; set;}

[Required (Errormessage= ("Please enter your phone number")]
public string Phone {get; set;}

[Required (errormessage= "Please select your gender")]
public string Gender {get; set;}

}
}

Controllers\homecontroller.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using Mvchelloworld.models;

Namespace Mvchelloworld.controllers
{
public class Homecontroller:controller
{
//
GET:/home/

Public ActionResult Index ()
{
return View ();
}

       ////system default requests are routed to HomeController and index this action
        //public string Index ()
       //{
        //   //write string directly to the client, you can not add index.cshtml page
        //    return "        //}

       //return non-string, need to add welcome.cshtml
         public ActionResult Welcome ()
        {
            //viewbag is a property of a dynamic type defined by the controller, which means you can add any property to him, The property of the dynamic type at compile time is not checked by the
           //page can be passed @ViewBag. XX Direct Call value
            viewbag.name = "Welcome action returns View to Show ViewBag name ";
            return View ();
       }


        public actionresult sendinformation (person person)
         {
            if ( Modelstate.isvalid)
            {
                 return Content ("Thank," + person . Name + "," + person. Email + "," + person. Phone + "," + person. Gender);
           }
            Else
             {
                 return View ("Index");
           }
       }

}
}

View-control-model base, strongly typed view, add validation Sample

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.