ASP.NET MVC 2 in Action読書ノート[4]
25272 ワード
Chapter04:
ControllerExamples
Index.aspx:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Display.aspx:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Edit.aspx:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
UserController.cs:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
ControllerExamples
Index.aspx:
<h2>Index</h2>
<%if (TempData.ContainsKey("message"))
{%>
<p class="input-validation-success"><%=TempData["message"]%></p>
<%} %>
<table>
<tr>
<th>
Username
</th>
<th>
Name
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.Username) %>
</td>
<td>
<%= Html.Encode(item.Name) %>
</td>
</tr>
<% } %>
</table>
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Display.aspx:
<h2>Display</h2>
<fieldset>
<legend>Fields</legend>
<p>
Username:
<%= Html.Encode(Model.Username) %>
</p>
<p>
Name:
<%= Html.Encode(Model.Name) %>
</p>
</fieldset>
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Edit.aspx:
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Username">Username:</label>
<%= Html.TextBox("Username", Model.Username) %>
<%= Html.ValidationMessage("Username", "*") %>
</p>
<p>
<label for="FirstName">FirstName:</label>
<%= Html.TextBox("FirstName", Model.FirstName) %>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">LastName:</label>
<%= Html.TextBox("LastName", Model.LastName) %>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
UserController.cs:
public class UserController : Controller
{
public ActionResult Index()
{
IEnumerable<User> users =
UserRepository.GetAll();
UserDisplay[] viewModel = users.Select(
user => new UserDisplay
{
Username = user.Username,
Name =
user.FirstName + " " +
user.LastName
}).ToArray();
return View(viewModel);
}
[HttpGet]
public ActionResult Display(int Id)
{
User user = UserRepository.GetById(Id);
var viewModel = new UserDisplay
{
Username = user.Username,
Name =
user.FirstName + " " +
user.LastName
};
return View(viewModel);
}
[HttpGet]
public ActionResult Edit(int Id)
{
User users = UserRepository.GetById(Id);
var viewModel = new UserInput
{
Username =
users.Username,
FirstName =
users.FirstName,
LastName =
users.LastName,
};
return View(viewModel);
}
[HttpPost]
public ActionResult Edit(UserInput input)
{
if (ModelState.IsValid)
{
UpdateUserFromInput(input);
TempData["message"] = "The user was updated";
return RedirectToAction("index");
}
return View(input);
}
private void UpdateUserFromInput(UserInput input)
{
User user =
UserRepository.GetByUsername(input.Username);
user.FirstName = input.FirstName;
user.LastName = input.LastName;
UserRepository.Save(user);
}
}
public class UserDisplay
{
public string Username { get; set; }
public string Name { get; set; }
}
public class UserInput
{
[Required]
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public static class UserRepository
{
public static IEnumerable<User> GetAll()
{
return new[]
{
new User
{
Username = "FFlintstone",
FirstName = "Fred",
LastName = "Flintstone"
},
new User
{
Username = "brubble",
FirstName = "Barney",
LastName = "Rubble"
},
new User
{
Username = "bbrubble",
FirstName = "Bamm-Bamm",
LastName = "Rubble"
},
new User
{
Username = "wilma",
FirstName = "Wilma",
LastName = "Flintstone"
},
new User
{
Username = "pebbles",
FirstName = "Pebbles",
LastName = "Flintstone"
},
new User
{
Username = "dino",
FirstName = "Dino",
LastName = "Flintstone"
},
};
}
public static User GetById(int id)
{
if (id == 2)
return new User();
return new User
{
Username = "FFlintstone",
FirstName = "Fred",
LastName = "Flintstone"
};
}
public static User GetByUsername(string username)
{
return
GetAll().Where(s => s.Username == username).
FirstOrDefault();
}
public static void Save(User user)
{
}
}
public class User
{
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }