1: the first is the Model layer (Model is not just an entity layer, but also a common layer-3 BLL. The DAL code is written here! Public class BLLStuedent {}):
Using System. ComponentModel. DataAnnotations; // This is a verified space class; four different types;
Namespace MvcLogin. Models
{
Public class Student
{
[Required (ErrorMessage = "ID cannot be blank")]
Public int Id {get; set ;}
[Required (ErrorMessage = "name cannot be blank")]
Public string Name {get; set ;}
[RegularExpression ("^ [\ u7537 \ u5973] + $", ErrorMessage = "gender only supports" male "or" female "")]
Public string Sex {get; set ;}
[Range (1,120, ErrorMessage = "age must be between 1 and")]
Public int Age {get; set ;}
}
}
2: controller code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using MvcLogin. Models;
Namespace MvcLogin. Controllers
{
Public class StudentController: Controller
{
Static List <Student> students = new List <Student> ();
//
// GET:/Student/
Public ActionResult Index ()
{
Return View (students );
}
//
// GET:/Student/Details/5
Public ActionResult Details (Student s)
{
Return View (s );
}
//
// GET:/Student/Create
Public ActionResult Create ()
{
Return View ();
}
//
// POST:/Student/Create
[HttpPost]
Public ActionResult Create (Student s)
{
Try
{
// TODO: Add insert logic here
If (! ModelState. IsValid)
{
Return View ("Create", s );
}
Students. Add (s );
Return RedirectToAction ("Index ");
}
Catch
{
Return View ();
}
}
//
// GET:/Student/Edit/5
Public ActionResult Edit (int id)
{
Student s = new Student ();
Foreach (Student stu in students)
{
If (stu. Id = id)
{
S. Id = stu. Id;
S. Name = stu. Name;
S. Sex = stu. Sex;
S. Age = stu. Age;
}
}
Return View (s );
}
//
// POST:/Student/Edit/5
[HttpPost]
Public ActionResult Edit (Student s)
{
Try
{
// TODO: Add update logic here
If (! ModelState. IsValid)
{
Return View ("Edit", s );
}
Foreach (Student stu in students)
{
If (stu. Id = s. Id)
{
Stu. Id = s. Id;
Stu. Name = s. Name;
Stu. Sex = s. Sex;
Stu. Age = s. Age;
}
}
Return RedirectToAction ("Index ");
}
Catch
{
Return View ();
}
}
//
// GET:/Student/Delete/5
Public ActionResult Delete (int id)
{
Student s = new Student ();
Foreach (Student stu in students)
{
If (stu. Id = id)
{
S. Id = stu. Id;
S. Name = stu. Name;
S. Sex = stu. Sex;
S. Age = stu. Age;
}
}
Return View (s );
}
//
// POST:/Student/Delete/5
[HttpPost]
Public ActionResult Delete (Student s)
{
Try
{
// TODO: Add delete logic here
Foreach (Student stu in students)
{
If (stu. Id = s. Id)
{
Students. Remove (stu );
Break;
}
}
Return RedirectToAction ("Index ");
}
Catch
{
Return View ();
}
}
}
}
3: view code:
Code on the list page:
<Table>
<Tr>
<Th> </th>
<Th>
Id
</Th>
<Th>
Name
</Th>
<Th>
Sex
</Th>
<Th>
Age
</Th>
</Tr>
<% Foreach (var item in Model) {%>
<Tr>
<Td>
<%: Html. ActionLink ("Edit", "Edit", new {id = item. Id}) %> |
<%: Html. ActionLink ("Details", "Details", item) %> | // Special notes
<%: Html. ActionLink ("Delete", "Delete", new {id = item. Id}) %>
</Td>
<Td>
<%: Item. Id %>
</Td>
<Td>
<%: Item. Name %>
</Td>
<Td>
<%: Item. Sex %>
</Td>
<Td>
<%: Item. Age %>
</Td>
</Tr>
<% }%>
</Table>
<P>
<%: Html. ActionLink ("Create New", "Create") %>
</P>
<Br/>
Added code:
<% Using (Html. BeginForm () {%>
<%: Html. ValidationSummary (true) %>
<Fieldset>
<Legend> Fields </legend>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Id) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Id) %>
<%: Html. ValidationMessageFor (model => model. Id) %>
</Div>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Name) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Name) %>
<%: Html. ValidationMessageFor (model => model. Name) %>
</Div>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Sex) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Sex) %>
<%: Html. ValidationMessageFor (model => model. Sex) %>
</Div>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Age) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Age) %>
<%: Html. ValidationMessageFor (model => model. Age) %>
</Div>
<P>
<Input type = "submit" value = "Create"/>
</P>
</Fieldset>
<% }%>
<Div>
<%: Html. ActionLink ("Back to List", "Index") %>
</Div>
Edited code:
<% Using (Html. BeginForm () {%>
<%: Html. ValidationSummary (true) %>
<Fieldset>
<Legend> Fields </legend>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Name) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Name) %>
<%: Html. ValidationMessageFor (model => model. Name) %>
</Div>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Sex) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Sex) %>
<%: Html. ValidationMessageFor (model => model. Sex) %>
</Div>
<Div class = "editor-label">
<%: Html. LabelFor (model => model. Age) %>
</Div>
<Div class = "editor-field">
<%: Html. TextBoxFor (model => model. Age) %>
<%: Html. ValidationMessageFor (model => model. Age) %>
</Div>
<P>
<Input type = "submit" value = "Save"/>
</P>
</Fieldset>
<% }%>
<Div>
<%: Html. ActionLink ("Back to List", "Index") %>
</Div>