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