Asp.Net+Mvc3.0+Demo
超簡易mvc 3の例ここで論理をcontrollerと書くのは実際にはそうすべきではない.
超簡易mvc 3インスタンスここではロジックをcontrollerと書くのは実際にはそうすべきではない
1枚の表UserInfo{UserID,UserName,UserPassword}論理的にcontrollerに書いたモデルには1つのエンティティが記載されていませんHomeController
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TestDemo.Models; using System.Data.Objects;
namespace TestDemo.Controllers { public class HomeController : Controller { // //GET:/Home/
public ActionResult Index() { TestEntities te = new TestEntities(); ObjectQuery<UserInfo> userInfos = te.UserInfoes;
var users = from u in userInfos select u;
ViewData.Model = users.ToList();
return View(); }
public ActionResult Delete(int id) { TestEntities te = new TestEntities(); ObjectQuery<UserInfo> userInfos = te.UserInfoes;
var query = userInfos.Single(u => u.UserID == id);
te.DeleteObject(query);
te.SaveChanges();
return RedirectToAction("Index"); }
public ActionResult Add() { return View(); }
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Add(FormCollection form) { TestEntities te = new TestEntities();
UserInfo addUser = new UserInfo() { UserName = form["userName"].ToString(), UserPassword = form["userPassword"].ToString() };
te.UserInfoes.AddObject(addUser);
te.SaveChanges();
return RedirectToAction("Index"); }
public ActionResult Edit(int id) { TestEntities te = new TestEntities(); ObjectQuery<UserInfo> userInfos = te.UserInfoes;
UserInfo user = userInfos.Single(u => u.UserID == id);
ViewData.Model = user;
return View(); }
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(FormCollection form) { TestEntities te = new TestEntities(); ObjectQuery<UserInfo> userInfos = te.UserInfoes;
int id = Int32.Parse( form["userID"].ToString());
var query = from u in userInfos where u.UserID == id select u;
UserInfo editUser = query.First();
UserInfo user = new UserInfo() { UserID=id, UserName = form["userName"].ToString(), UserPassword = form["userPassword"].ToString() };
editUser.UserName = user.UserName; editUser.UserPassword = user.UserPassword;
te.SaveChanges();
return RedirectToAction("Index");
}
} }Add.cshtml @{ ViewBag.Title = "Add"; } <h2>Add</h2> @using (Html.BeginForm()) { @Html.Label(" ") @Html.TextBox("userName") <hr /> @Html.Label(" ") @Html.TextBox("userPassword") <input type="submit" value=" "/> } Edit.cshtml @using TestDemo.Models @{ ViewBag.Title = "Edit"; } <h2> Edit</h2> @using (Html.BeginForm()) { UserInfo query = ViewData.Model; @Html.Label("ID", query.UserID.ToString()); <br /> @Html.Label(" "); @Html.TextBox("userName");<br /> @Html.Label(" "); @Html.TextBox("userPassword");<br /> @Html.Hidden("userID", query.UserID); <input type="submit" value=" " /> } Index.cshtml @using TestDemo.Models; @{ ViewBag.Title = "Index"; } <!DocType html /> <html> <head> <title></title> <link href="@Url.Content("../Content/Test.css")" type="text/css" rel="Stylesheet" /> </head> <body> <div class="all" > <div class="top"> </div> <div class="content"> @{ List<UserInfo> users = ViewData.Model; @Html.ActionLink(" ", "Add") <table id="qzw"> @foreach (UserInfo user in users) { <tr> <td>@user.UserID </td> <td>@user.UserName </td> <td>@user.UserPassword </td> <td>@Html.ActionLink(" ", "Delete", new { id = user.UserID }) </td> <td>@Html.ActionLink(" ", "Edit", new { id = user.UserID }) </td> </tr> } </table> } </div> </div> </body> </html>