首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Html.DropDownlistFor selectedValue

Html.DropDownlistFor selectedValue
EN

Stack Overflow用户
提问于 2011-10-28 14:32:08
回答 1查看 2.6K关注 0票数 0

很难让这个dropDownList绑定。这是一个简单的州清单。Model.State是"TX",但我的下拉列表显示"AK",这是列表中的第一个值。有什么想法吗?

代码语言:javascript
复制
<%: Html.DropDownListFor(
                 model => model.State,
                 new SelectList(
                               muniSynd.getStatecodesDropDown(),
                               "Value",
                               "Text",
                               Model.State.ToString().Trim()
                 )
              )

            %>

在我的muniSynd类中,它是我的dataContext的包装器……

代码语言:javascript
复制
public IList<StateCodeViewModel> getStatecodesDropDown()
        {
            var states = from p in this._MuniDc.Statecodes
                         select new StateCodeViewModel
                         {
                             Value = p.Statecode1.ToString().Trim(),
                             Text = p.Statecode1.ToString().Trim()
                         };
            return states.ToList();
        }



public class StateCodeViewModel
    {
        public string Value{get;set;}
        public string Text{get;set;}
    }

我在使用LinqToSQL,这是一个对象

代码语言:javascript
复制
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Statecodes")]
    public partial class Statecode
    {

        private string _Statecode1;

        public Statecode()
        {
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Name="Statecode", Storage="_Statecode1", DbType="NVarChar(3)")]
        public string Statecode1
        {
            get
            {
                return this._Statecode1;
            }
            set
            {
                if ((this._Statecode1 != value))
                {
                    this._Statecode1 = value;
                }
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2011-10-28 15:47:14

我试图复制您的问题,但下面的示例运行得很好:

代码语言:javascript
复制
public class HomeController : Controller
{
     public ActionResult Index()
     {
         var viewModel = new IndexViewModel();

         viewModel.State = "TX";

         return this.View(viewModel);
     }

         public static IList<StateCodeViewModel> getStatecodesDropDown()
         {
             var stateCodes = new List<string> { "AX", "GH", "TX" };

             var states = from p in stateCodes
                          select new StateCodeViewModel
                          {
                              Value = p,
                              Text = p
                          };
             return states.ToList();
         }
    }

    public class IndexViewModel
    {
        public string State { get; set; }
    }

    public class StateCodeViewModel
    {
        public string Value { get; set; }
        public string Text { get; set; }
    }

视图

代码语言:javascript
复制
@using MVCWorkbench.Controllers
@model IndexViewModel

@{
    ViewBag.Title = "title";
}

@Html.DropDownListFor(model => model.State,
                      new SelectList(HomeController.getStatecodesDropDown(),
                               "Value",
                               "Text",
                               Model.State.ToString().Trim()
                 )
              )

不知道其他的建议是什么,然后检查国家值是否在下拉列表中。也许这也是一个对案件敏感的问题?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7930624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档