【问题标题】:Html.DropDownlistFor selectedValueHtml.DropDownlistFor selectedValue
【发布时间】:2011-10-28 23:47:14
【问题描述】:

很难将此 dropDownList 绑定。这只是一个状态列表。 Model.State 是“TX”,但我的下拉列表显示“AK”,这是列表中的第一个值。有什么想法吗?

<%: Html.DropDownListFor(
                 model => model.State,
                 new SelectList(
                               muniSynd.getStatecodesDropDown(),
                               "Value",
                               "Text",
                               Model.State.ToString().Trim()
                 )
              )

            %>

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

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,这里是对象

[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;
                }
            }
        }
    }

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    我已尝试复制您的问题,但下面的示例运行良好:

    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; }
        }
    

    查看

    @using MVCWorkbench.Controllers
    @model IndexViewModel
    
    @{
        ViewBag.Title = "title";
    }
    
    @Html.DropDownListFor(model => model.State,
                          new SelectList(HomeController.getStatecodesDropDown(),
                                   "Value",
                                   "Text",
                                   Model.State.ToString().Trim()
                     )
                  )
    

    不确定其他建议是什么,然后检查状态值是否在下拉列表中。这也可能是区分大小写的问题?

    【讨论】:

    • 首先,感谢您花费这么多时间。使用 linq 时,我意识到状态返回为“TX”。我以为修剪会解决这个问题。如果你改变你的 viewModel.State = "TX ";
    • 您好。如果您保持名称一致,那么是的,默认 MVC 模型绑定器将能够匹配 POST 期间的值。至于 Trim( 不起作用,我不确定那里发生了什么。代码看起来很好。不明白为什么 Model.State 仍然有一个空间。
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多