MVC 学习系列文章(三)Controller And Action

HomeController:

View Code
public class HomeController : Controller
{
public ActionResult Index()
{
return this.View();
}

//跳转至WebSite
public ActionResult RedirectToGoogle()
{
return this.Redirect("http://www.google.com.hk/");
}

//跳转至Action
public ActionResult RedirectToEmployeesIndex()
{
return this.RedirectToAction("Index", "Employees");
}

//默认情况下,MVC 框架将 controller 类的所有公共方法都视为操作方法。
//如果您的 controller 类包含公共方法,并且您不希望它成为操作方法,
//则必须用 NonActionAttribute 特性标记该方法。
[NonAction]
public ActionResult NonActionMethod()
{
return this.View();
}

//处理未知Action异常
protected override void HandleUnknownAction(string actionName)
{
base.HandleUnknownAction(actionName);
}
}

Home.Index:

View Code
@{
ViewBag.Title = "Home";
}
<h2>
Home</h2>
@Html.ActionLink("Google", "RedirectToGoogle")
@Html.ActionLink("Employees.Index", "RedirectToEmployeesIndex")

EmployeesController:

View Code
public class EmployeesController : Controller
{
public ActionResult Index()
{
var northwind = DataContextFactory.CreateNorthwind();
return View(northwind.Employees);
}

//处理参数,id是Route参数,birthDate是QueryString
public ActionResult Details(int id, DateTime? birthDate)
{
return View("Details", this.GetEmployeeById(id));
}

//处理参数,处理Form参数传递
public ActionResult Save()
{
var northwind = DataContextFactory.CreateNorthwind();
int iEmployeeID = Convert.ToInt32(Request.Form["EmployeeID"].Trim());
Employee employee = (from e in northwind.Employees
where e.EmployeeID == iEmployeeID
select e).First<Employee>();
employee.FirstName = Request.Form["FirstName"].Trim();
employee.LastName = Request.Form["LastName"].Trim();
employee.HomePhone = Request.Form["HomePhone"].Trim();
northwind.SubmitChanges();

return View("Details", employee);
}

private Employee GetEmployeeById(int id)
{
var northwind = DataContextFactory.CreateNorthwind();
Employee employee = (from e in northwind.Employees
where e.EmployeeID == id
select e).First<Employee>();
return employee;
}
}

Employees.Index:

View Code
@using MVCDemo.Example02.Models;
@{
ViewBag.Title = "Employees";
}
<h2>
Employees</h2>
<ul>
@foreach (Employee employee in this.Model)
{
<li>
@Html.ActionLink(employee.FirstName, "Details", new
{
id = employee.EmployeeID,
birthDate = employee.BirthDate
})
</li>
}
</ul>

Employees.Details:

View Code
@model MVCDemo.Example02.Models.Employee
@{
ViewBag.Title = "Details";
}
<h2>
Details</h2>
@using (Html.BeginForm("Save", "Employees"))
{
@Html.ValidationSummary(true)
<table>
<tr>
<td>@Html.LabelFor(model => model.EmployeeID)</td>
<td>@Html.DisplayFor(model => model.EmployeeID)</td>
<td></td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.FirstName)</td>
<td>@Html.TextBoxFor(model => model.FirstName)</td>
<td>@Html.ValidationMessageFor(model => model.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.LastName)</td>
<td>@Html.TextBoxFor(model => model.LastName)</td>
<td>@Html.ValidationMessageFor(model => model.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.HomePhone)</td>
<td>@Html.TextBoxFor(model => model.HomePhone)</td>
<td>@Html.ValidationMessageFor(model => model.HomePhone)</td>
</tr>
</table>
<input type="submit" value="Submit" />
}
@Html.ActionLink("Back", "Index")

若有疑问或不正之处,欢迎提出指正和讨论。

posted @ 2012-01-31 09:57  iceknp  阅读(387)  评论(0编辑  收藏  举报