一天学会MVC3之传递模型数据到视图

右键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"/>
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值