一天学会MVC3之传递模型数据到视图
来源:互联网 发布:农村淘宝政策 编辑:程序博客网 时间:2024/05/02 05:00
右键Models文件夹,添加类:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcMusicStore.Models{ public class Genre { public string Name { get; set; } }}
继续添加类:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcMusicStore.Models{ public class Album { public string Title { get; set; } public Genre Genre { get; set; } }}
修改StoreController.cs的Details方法:
// // GET: /Store/Details/5 public ActionResult Details(int id) { var album = new Album { Title = "Album " + id }; return View(album); }
然后在这个方法上添加视图模板:
点击添加按钮后代码如下:
@model MvcMusicStore.Models.Album@{ ViewBag.Title = "Details";}<h2>@Model.Title</h2>
运行程序,输入:http://localhost:58716/Store/Details/5
下面我们修改StoreController.cs的 Index()方法:
public ActionResult Index() { var genres = new List<Genre> { new Genre { Name = "Disco"}, new Genre { Name = "Jazz"}, new Genre { Name = "Rock"} }; return View(genres); }
右键单击该方法
改变Index.cshtml的代码如下:
@model IEnumerable<MvcMusicStore.Models.Genre>@{ViewBag.Title = "Store"; }<h3> Browse Genres</h3><p> Select from @Model.Count() genres:</p><ul> @foreach (var genre in Model) { <li>@genre.Name</li> }</ul>
浏览应用程序:
下面我们要添加一些超连接改变代码如下:
@model IEnumerable<MvcMusicStore.Models.Genre>@{ViewBag.Title = "Store"; }<h3> Browse Genres</h3><p> Select from @Model.Count() genres:</p><ul> @foreach (var genre in Model) { <li>@Html.ActionLink(genre.Name, "Browse", new { genre = genre.Name })</li> }</ul>
运行http://localhost:58716/Store后,输出的代码类似:
<ul> <li><a href="/Store/Browse?genre=Disco">Disco</a></li> <li><a href="/Store/Browse?genre=Jazz">Jazz</a></li> <li><a href="/Store/Browse?genre=Rock">Rock</a></li></ul>
==================下拉选中的问题=====================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace TestMvc.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
items.Add(new SelectListItem { Text = "Jade", Value = "28"});
items.Add(new SelectListItem { Text = "Yao", Value = "24" });
ViewBag.dd1 = items;
return View();
}
//必须要传EmployeeModel模型过来,否则下拉会选不中
[HttpPost]
public ActionResult Index(EmployeeModel model)
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
items.Add(new SelectListItem { Text = "Jade", Value = "28" });
items.Add(new SelectListItem { Text = "Yao", Value = "24" });
ViewBag.dd1 = items;
return View();
}
}
public class EmployeeModel
{
public SearchModel SearchModels { get; set; }
public String Value { get; set; } //必须与上面下拉选项对应Value
}
public class SearchModel
{
public String Value { get; set; }//用到的话必须与上面下拉选项对应Value
}
}
@model TestMvc.Controllers.EmployeeModel
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.Value, ViewBag.dd1 as IList<SelectListItem>, new { @class = "form-control select" })
//也可以下面这样
@Html.DropDownListFor(model => model.SearchModels.Value, ViewBag.dd1 as IList<SelectListItem>, new { @class = "form-control select" })
<input type="submit"/>
}
- 一天学会MVC3之传递模型数据到视图
- 一天学会MVC3之Views and Models(视图和模型)
- 一天学会MVC3之模型验证中的数据注解
- 一天学会MVC3之数据访问
- 一天学会MVC3之开篇
- 一天学会MVC3之Scaffolding
- 一天学会MVC3之Controllers(控制器)
- Spring MVC 传递模型数据到视图中方法总结
- mvc3 怎么传递非 模型 数据 给post方法
- ASP.NET MVC3入门教程之参数(数据)传递
- 把数据从控制器传递到视图
- SpringMVC-3 模型数据到视图
- ASP.NET MVC3学习心得-----视图和模型
- spring实战-将模型传递到视图中渲染
- ASP.NET MVC3书店--第三节+视图与视图模型
- cakephp3 如何从控制器传递数据到视图
- ASP.NET MVC3学习--视图和模型(4)
- ASP.NET MVC3学习--视图和模型(1)
- AAuto table数据类型的速度测试
- 浅谈散列
- code
- 为了我,母亲的病情加重了
- 硬件设计参考网站
- 一天学会MVC3之传递模型数据到视图
- 移动应用开发技术选择六要素
- linux消息队列
- 浅谈二进制文件读写和文本文件读写的区别
- PlayBook应用安装工具
- C++ GUi 库介绍
- 第四章字符串string,bytes
- USACO Section 5.2 Wisconsin Squares - 按要求DFS就行了..
- 关于Android系统调试程序经常出现的错误解决方法(一)