Html.DropDownlist для выбранного значения - PullRequest
0 голосов
/ 28 октября 2011

С трудом доставляет этот 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 голосов
/ 28 октября 2011

Я пытался повторить вашу проблему, но мой пример ниже работает нормально:

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

Не уверен, что предложить другимзатем проверьте, что значение State находится в раскрывающемся списке.Также это может быть проблема чувствительности к регистру, возможно?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...