MVC 學習系列文章(三)Controller And Action

來源:互聯網
上載者:User

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")

若有疑問或不正之處,歡迎提出指正和討論。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.