Question

I have a custom config file that holds the basic information for a group of databases I might connect to with my MVC application. It is a data editing application, and the user selects which database contains the records they will edit before we search for the individual record. In order to create the dropdown the user will use, I have the following classes. The key value is a unique string, because the MuseSiteNumber could be the same for more than one of the items in the list:

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

The first thing I do in my controller is read the configuration into a list of sites, then load it into the IndexViewModel, and send it to the view for the dropdown. My view model is being populated in the HttpGet, but when I try to get the selected value out, it is returned as null.

What am I missing? Is this because I can't use a string as a Key?

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();
    }
}
Was it helpful?

Solution

in a drop down list for the selected value is saved to the parameter in the lambda expression. You have your view model there. What you need to do is add a selected parameter to your view model

public string Selected { get; set; }

then on your view change your lambda to look at that value

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

on your get you can preset the drop down by setting Selected and the selected value will be saved to that field

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top