Html.DropDownlistFor selectedValue

时间:2011-10-28 14:32:08

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

很难将此dropDownList绑定到Bind。这只是一个州列表。 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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我试图复制你的问题,但下面的示例运行正常:

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()
                 )
              )

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