0

I Have a model, which is a List of some objects. Every object, has a "description" filed. Now, I want a dropdown list with all the descriptions, that inside this list.

    public ActionResult PopPage(string line)
    {
        List<Campaign> CampaignList = new List<Campaign>();
        SetLogFiles();            
        try
        {
            ViewBag.CLID = line;
            CampaignList = DBAccess.GetCampaigns();
        }
        catch (Exception ex)
        {
            Logger.WriteError(ex);
        }

        return View("LineStatePopUp",CampaignList);

    }

In the top of the view:

@model List<ClickServer.Models.Campaign>

1 Answer 1

1

You are sending the list of campaigns as model to your view. That's fine. But how are you going to get the value that the user would select? For that you should have a property at you server side binded to the selected item at client-side.

I would suggest using a ViewModel which would have the list of campaigns as collection of SelectListItem (which would be populated in the controller) and another property for the selected item and then instead of passing the list of campaigns directly to your view you should pass the new viewModel and bind accordingly.

ViewModel

public class CampaignsViewModel
{
    public int SelectedCampaignId { get; set; }
    public IEnumerable<SelectListItem> Campaigns{ get; set; }
}

View

@model CampaignsViewModel
@Html.DropDownListFor(m => m.SelectedCampaignId , Model.Campaigns)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.