标签:
http://bbs.csdn.net/topics/390867060
由于不知道错误原因在哪 我尽量把代码都贴出来吧:
重点是:在 Controller 类里 我给 SelectListItem集合的 某项 Selected 赋值为TRUE 在视图中就应该 将该项选中吧?? 在我这怎么没还是呢? 没有任何项选中!!
Controller 类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
using S01MVC.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace S01MVC.Controllers { public class LkfValidateController : Controller { // // GET: /LkfValidate/ public ActionResult Index() { LkfValidateData model = new LkfValidateData() { InCome = 1000000, LastLoginTime = DateTime.Now.AddDays(-5), UserPassword = "12345678" , UserName = "郭德纲" , UserID = 1 }; List<SelectListItem> list = new List<SelectListItem>(); list.Add( new SelectListItem() { Text = "500元以下" , Value = "500" }); list.Add( new SelectListItem() { Text = "1000元以下" , Value = "1000" }); list.Add( new SelectListItem() { Text = "一万元以下" , Value = "10000" }); list.Add( new SelectListItem() { Text = "十万元以下" , Value = "100000" }); list.Add( new SelectListItem() { Text = "一百万元以下" , Value = "1000000" , Selected = true }); ViewData[ "income" ] = list.AsEnumerable(); return View(model); } } } |
Views 视图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@model S01MVC.Models.LkfValidateData @{ Layout = null ; } <!DOCTYPE html> <html> <head> <meta name= "viewport" content= "width=device-width" /> <title>Index</title> <style type= "text/css" > #tbinfo{border:1px solid #0094ff; margin:auto} #tbinfo td{ border: 1px solid #0094ff; } </style> </head> <body> <div> @ using (Html.BeginForm( "edit" , "LkfValidateData" ,FormMethod.Post)) { IHtmlString stralogn = Html.Raw( "align=\"right\"" ); <table id= "tbinfo" > <tr><td align= "right" >@Html.LabelFor(c=>c.UserID)</td><td>@Model.UserID</td></tr> <tr><td @stralogn>@Html.LabelFor(c => c.UserName)</td><td>@Html.TextBoxFor(c => c.UserName) </td></tr> <tr><td @stralogn>@Html.LabelFor(c => c.UserPassword)</td><td>@Html.EditorFor(c=>c.UserPassword)</td></tr> <tr><td @stralogn>@Html.LabelFor(c => c.LastLoginTime)</td><td>@Html.DisplayTextFor(c=>c.LastLoginTime)</td></tr> <tr><td @stralogn>@Html.LabelFor(c => c.InCome)</td><td>@Html.DropDownListFor(c=>c.InCome,ViewData[ "income" ] as IEnumerable<SelectListItem>)</td></tr> </table> } </div> </body> </html> |
模型类:LkfValidateData
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
namespace S01MVC.Models { public class LkfValidateData { [DisplayNameAttribute( "用户ID" )] public long UserID { get ; set ; } [DisplayName( "用户名" )] public string UserName { get ; set ; } [DisplayName( "用户密码" )] [DataTypeAttribute(DataType.Password)] public string UserPassword { get ; set ; } [DisplayName( "最后登录时间" )] public DateTime? LastLoginTime { get ; set ; } [DisplayName( "收入" )] public decimal InCome { get ; set ; } } } |
MVC 中@Html.DropDownListFor() 设置选中项 这么不好使 ? [问题点数:40分,结帖人lkf181]
标签:
原文地址:http://www.cnblogs.com/zkwarrior/p/5138014.html