MVC 自动装配
//HelloController.cs
using FirstMVC.Models;
using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FirstMVC.Controllers
{
public class HelloController : Controller
{
//
// GET: /Hello/
public ActionResult Index()
{
return View();
}
public ActionResult Say()
{
return Content("abc");
}
public ActionResult RedirectTest()
{
return Redirect(Url.Action("Index","Hello"));
}
public ActionResult JsonTest()
{
Person p = new Person()
{
Age = 10,
QQ = "123"
};
return Json(p, JsonRequestBehavior.AllowGet);
}
public ActionResult Add()
{
ViewBag.id = Request["id"];
return View();
}
[HttpPost]
public ActionResult Add(int id)//自动装配
{
ViewBag.id2 = id;
return View();
}
public ActionResult AddPerson()
{
return View();
}
[HttpPost]
public ActionResult AddPerson(Person p)//自动装配
{
ViewData.Model = p;
return View("AddPerson1");
}
}
}
//AddPerson.cshtml
@model FirstMVC.Models.Person
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AddPerson</title>
</head>
<body>
<div>
@using (Html.BeginForm("AddPerson", "Hello", FormMethod.Post))
{
<span>Age:</span>@Html.TextBoxFor(p => p.Age);<br />
<span>QQ:</span>@Html.TextBoxFor(p => p.QQ); <br />
<input type="submit" name="name" value=" submit" />
}
</div>
</body>
</html>