这是数据字典(绝对不会有错):
1 private static Dictionary<string, string> isChecked = null; 2 /// <summary> 3 /// 关键字类型 4 /// </summary> 5 public static Dictionary<string, string> IsCheck 6 { 7 get 8 { 9 if (isChecked == null) 10 { 11 isChecked = new Dictionary<string, string>(); 12 isChecked.Add("0", "否"); 13 isChecked.Add("1", "是"); 14 } 15 return isChecked; 16 } 17 }
页面访问(强类型视图):
1 <div class="editor-field"> 2 是否在职: 3 <select> 4 <option value="-1">-请选择-</option> 5 @{ 6 var isJob = FrameComBLL.Dicts.IsCheck; 7 foreach (var item in isJob) 8 { 9 Response.Write(string.Format("<option value='{0}'>{1}</option>", item.Key, item.Value)); 10 } 11 } 12 </select> 13 </div>
我觉得理论上应该可以输出的,但是为什么输出的结果是:
就是这个选择框应该是在一起的,怎么会就这么分开
发送到浏览器代码:
就是分开的(body直接进去就是option,为什么呢,在线等待,谢谢)我想了下是不是model的问题呢?请告知
1 <option value="0">否</option> 2 <option value="1">是</option> 3 <meta charset="utf-8"> 4 <meta content="width=device-width" name="viewport"> 5 <title>UserReg</title> 6 <link rel="stylesheet" href="/Content/site.css"> 7 <script src="/Scripts/modernizr-2.6.2.js"> 8 <script type="text/javascript"> 9 <h2>用户注册</h2> 10 <form method="post" action="/" novalidate="novalidate"> 11 <input type="hidden" value="0xU0uxwq7Y5Dnyb6RMeBy-RZSmweCqXO95nED9pR2vezJn0ENwezyLc9Nn7ev25eCwFMquvJQ7HgAbvYe9Sy-qxsTZwv1yGP0Zmv8c1Vu5I1" name="__RequestVerificationToken"> 12 <fieldset> 13 <legend>user</legend> 14 <div class="editor-field"> 15 <div class="editor-label"> </div> 16 <div class="editor-field"> 17 <p> 18 </fieldset> 19 </form> 20 <div> 21 <script id="__browserLink_initializationData" type="application/json"> 22 <script async="async" src="http://localhost:65271/0f3b22e38a684a6f80160875a114da41/browserLink" type="text/javascript"> 23 <script src="/Scripts/jquery-1.8.2.js"> 24 <script src="/Scripts/jquery.unobtrusive-ajax.js"> 25 <script src="/Scripts/jquery.validate.js"> 26 <script src="/Scripts/jquery.validate.unobtrusive.js"> 27 </body>
我用的是ADO.NET实体模型,直接用框架生成的增删改查
<div class="editor-field"> 2 是否在职: 3 <select> 4 <option value="-1">-请选择-</option> 5 @{ 6 var isJob = FrameComBLL.Dicts.IsCheck; 7 foreach (var item in isJob) 8 { 9 <option value=' @item.Key'>@item.Value</option> 10 } 11 } 12 </select> 13 </div>
就这样弄就可以了,别搞那么麻烦了。
这个问题解决了,但是我怎么把这个key赋值给isJob呢
<select>
@{ var isJob = FrameComBLL.Dicts.IsCheck; foreach (var item in isJob) { <option value="@item.Key ">@item.Value</option> } } </select> @Html.EditorFor(model => model.uIsJob)
怎么弄呢
或者说用jQuery专门给赋值,应该不需要这么麻烦吧,有没有什么好的办法呢
@_Vegetables:
<select> @{ var isJob = FrameComBLL.Dicts.IsCheck; foreach (var item in isJob) { isJob =item.Key; <option value="@item.Key ">@item.Value</option> } } </select> @Html.EditorFor(model => model.uIsJob)
@ArnoldZhang: 这里不应该是item.key赋值么,怎么直接把这个赋值了呢,不管怎么说,问题已经解决了,豆子先给您了,或者说哪里应该是uIsJob=item.key?