2014-05-12 87 views
0

我有一個自定義配置文件,它包含我可能連接到我的MVC應用程序的一組數據庫的基本信息。它是一個數據編輯應用程序,用戶在我們搜索單個記錄之前選擇哪個數據庫包含他們將要編輯的記錄。爲了創建用戶將使用的下拉列表,我有以下類。鍵值是一個唯一的字符串,因爲MuseSiteNumber可能是不止一個在列表中的項目相同:選擇在帖子上爲空。我不明白爲什麼

public class SiteSelection 
{ 
    public string Key { get; set; } 
    public string Description { get; set; } 
    public string DatabaseServer { get; set; } 
    public string DatabaseName { get; set; } 
    public int MuseSiteNumber { get; set; } 
} 

public class IndexViewModel 
{ 
    [Display(Name = "Select Site for Updates")] 
    public List<SiteSelection> MuseSites { get; set; } 
    [Required] 
    public SiteSelection SelectedSite { get; set; } 
} 

我在我的控制器做的第一件事是讀取配置到站點列表,然後將其加載到IndexViewModel中,並將其發送到下拉視圖。我的視圖模型正在HttpGet中填充,但是當我嘗試獲取選定的值時,它將返回爲null。

我錯過了什麼?這是因爲我不能使用一個字符串作爲一個關鍵?

View: (Index.cshtml) 

@model MuseCorrecter.Models.IndexViewModel 
@{ 
@ViewBag.Title 
} 
<section> 
    <div class="content-wrapper"> 
     <hgroup class="title"> 
      @*<h2>@ViewBag.Message</h2>*@ 
     </hgroup> 
     @using (Html.BeginForm()) 
     { 

      @Html.DropDownListFor(h => h.SelectedSite, new SelectList(Model.MuseSites, "Key", "Description"), "Select one") 
     <div class="buttongroup" style="margin-top: 50px"> 
      <input type="submit" name="submitButton" value="Select" /> 
      <button type="button" onclick="location.href='@Url.Action("Index", "Home")'">Cancel</button> 
     </div> 
     } 
    </div> 
</section> 

Home Controller actions: 

public class HomeController : Controller 
{ 
    private static readonly ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 

    private ConfigReader ConfigReader { get; set; } 
    private List<SiteSelection> SelectableMuseSites { get; set; } 

    private List<SiteSelection> GetConfig() 
    { 
     ConfigReader = new ConfigReader(); 
     Logger.DebugFormat(" Current Environment {0} ", ConfigReader.Profile.Key); 

     SelectableMuseSites = new List<SiteSelection>(); 

     MuseSites ms = ConfigReader.MuseSites; 
     foreach (MuseSite s in ms) 
     { 
      SiteSelection ssvm = new SiteSelection(); 
      ssvm.Key = s.Key; 
      ssvm.DatabaseName = s.DatabaseName; 
      ssvm.DatabaseServer = s.DatabaseServer; 
      ssvm.Description = s.Description; 
      ssvm.MuseSiteNumber = s.MuseSiteNumber; 
      SelectableMuseSites.Add(ssvm); 

     } 

     Logger.DebugFormat(" Number of sites available : {0}", SelectableMuseSites.Count()); 

     return SelectableMuseSites; 
    } 

    [HttpGet] 
    public ActionResult Index() 
    { 
     ViewBag.Title = Constants.AppName; 
     ViewBag.Message = Constants.SiteSelectPrompt; 
     IndexViewModel ivm = new IndexViewModel(); 

     // get the sites from the configuration file to populate the view model 
     ivm.MuseSites = GetConfig(); 

     ViewBag.Message = ViewBag.Message + " (" + ConfigReader.Profile.Key.ToLower() + ")"; 

     return View(ivm); 
    } 

    [HttpPost] 
    public ActionResult Index(IndexViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      if (model.SelectedSite != null) 
      { 
       Console.Write("Got here!"); 
      } 
     } 
     return View(); 
    } 
} 

回答

0

在下拉列表中選定的值被保存到lambda表達式中的參數中。你有你的視圖模型。你需要做的是選擇的參數添加到您的視圖模型

public string Selected { get; set; } 

然後對你的看法改變你的lambda來看看在你拿到價值

@Html.DropDownListFor(h => h.Selected, new SelectList(Model.MuseSites, "Key", "Description"), "Select one") 

,你可以通過預設的下拉設置選擇和選定的值將被保存到該字段

+0

這就是我正在嘗試使用SelectedSite –

+1

選定的網站是一個複雜的模型,你不能在下拉列表中使用。它只將選定的值保存到一個對象 –

相關問題