2010-03-23 11 views
7

के लिए काम नहीं कर रहा है। मैं Html.DropDownListFor <> HtmlHelper का उपयोग करने की कोशिश कर रहा हूं और पोस्ट पर बाध्यकारी कुछ परेशानी कर रहा हूं। एचटीएमएल सही ढंग से प्रस्तुत करता है लेकिन सबमिट करते समय मुझे कभी भी "चयनित" मान नहीं मिलता है।MVC2 बाइंडिंग Html.DropDownListFor <>

<%= Html.DropDownListFor(m => m.TimeZones, 
           Model.TimeZones, 
           new { @class = "SecureDropDown", 
             name = "SelectedTimeZone" }) %> 

[Bind(Exclude = "TimeZones")] 
    public class SettingsViewModel : ProfileBaseModel 
    { 
     public IEnumerable TimeZones { get; set; } 
     public string TimeZone { get; set; } 

     public SettingsViewModel() 
     { 
      TimeZones = GetTimeZones(); 
      TimeZone = string.Empty; 
     } 

     private static IEnumerable GetTimeZones() 
     { 
      var timeZones = TimeZoneInfo.GetSystemTimeZones().ToList(); 
      return timeZones.Select(t => new SelectListItem 
         { 
          Text = t.DisplayName, 
          Value = t.Id 
         }); 
     } 
    } 

मैं कुछ अलग बातें करने की कोशिश की और यह सुनिश्चित करें कि मैं कुछ बेवकूफ कर रहा हूँ ... यह सिर्फ नहीं यकीन है कि क्या है :)

उत्तर

12

यहाँ एक उदाहरण मैं तुम्हें illustrating के लिए लिखा था है हूं DropDownListFor सहायक विधि का उपयोग:

मॉडल:

public class SettingsViewModel 
{ 
    public string TimeZone { get; set; } 

    public IEnumerable<SelectListItem> TimeZones 
    { 
     get 
     { 
      return TimeZoneInfo 
       .GetSystemTimeZones() 
       .Select(t => new SelectListItem 
       { 
        Text = t.DisplayName, Value = t.Id 
       }); 
     } 
    } 
} 

नियंत्रक:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new SettingsViewModel()); 
    } 

    [HttpPost] 
    public ActionResult Index(SettingsViewModel model) 
    { 
     return View(model); 
    } 
} 

दृश्य:

<% using (Html.BeginForm()) { %> 
    <%= Html.DropDownListFor(
     x => x.TimeZone, 
     Model.TimeZones, 
     new { @class = "SecureDropDown" } 
    ) %> 
    <input type="submit" value="Select timezone" /> 
<% } %> 

<div><%= Html.Encode(Model.TimeZone) %></div> 
+0

कि चाल किया था। यह क्या था कि मैं गलत कर रहा था? – devlife

+0

जैसा कि आपने अपने कोड का केवल एक हिस्सा दिखाया है, मैं यह नहीं कह सकता कि इसमें क्या गलत है। –

+0

मुझे लगता है कि मैंने क्या गलत किया। DropDownListFor (x => x.TimeZone) करने के बजाय मैंने इसे x.TimeZones के लिए किया था। मदद डारिन के लिए धन्यवाद। – devlife

संबंधित मुद्दे