帖子上的选择为空。我无法弄清楚原因

时间:2014-05-12 16:29:31

标签: c# asp.net-mvc-4 viewmodel

我有一个自定义配置文件,其中包含我可能与我的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();
    }
}

1 个答案:

答案 0 :(得分:0)

在所选值的下拉列表中保存到lambda表达式中的参数。你有自己的视图模型。您需要做的是将所选参数添加到视图模型

public string Selected { get; set; }

然后在你的视图中更改你的lambda以查看该值

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

在你得到的你可以通过设置选择预设下拉列表,所选的值将保存到该字段