C# selectedValue的Html.dropDownList

C# selectedValue的Html.dropDownList,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,很难绑定此dropDownList。这只是一个州的列表。Model.State是“TX”,但我的dropdownlist显示的是“AK”,这是列表中的第一个值。有什么想法吗 <%: Html.DropDownListFor( model => model.State, new SelectList( muniSynd.getStatecodesDropDo

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

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

            %>

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

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

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

首先,感谢您花了这么多时间在这方面。在使用linq时,我意识到状态返回为“TX”。我想修剪一下就可以了。如果更改viewModel.State=“TX”;您好。如果您保持名称一致,那么是的,默认的MVC模型绑定器将能够在
POST
期间匹配这些值。至于修剪(不起作用,我不确定那里发生了什么。代码看起来很好。不明白为什么
Model。State
仍会有一个空间。
[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;
                }
            }
        }
    }
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()
                 )
              )