Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

c# - Dropdown in MVC

In my MVC application, I need to add a dropdown that would show a list of domain names.

I already have a ViewModel that contains multiple properties. I am not sure what the sequence of the steps should be:

  1. Add a new property to my ViewModel? What should be the type? List?
  2. Define a method that populates the above property with values.
  3. Use that property in the View? Use HTML.DropdownFor?

I know I should put some code in my Question, but right now I am having difficulty getting started with this...

EDIT: Added the following property to the ViewModel:

 public IEnumerable<SelectListItem> DomainList { get; set; }

and implemented a method to return a list of Domains:

internal static List<Domain> FetchAllDomains()

Next in my controller action, I have:

var domains = FetchAllDomains().Select(d => d.DomainName);
return new EmailModel() {DomainList = domains };

But I get the following error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1) Add a new property to my ViewModel? What should be the type? List?

You need 2 properties to be more precise: an IEnumerable<SelectListItem> to hold all the available options and a scalar property to hold the selected value

2) Define a method that populates the above property with values.

Yes.

3) Use that property in the View? Use HTML.DropdownFor?

No, not in the view. The view doesn't call any methods. A view works with the view model. It is the responsibility of the controller to pass a properly filled view model to the view.

So for example:

public class MyViewModel
{
    public string SelectedValue { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }

    ... some other properties that your view might need
}

and then a controller action that will populate this view model:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.Values = new[]
    {
        new SelectListItem { Value = "1", Text = "item 1" },
        new SelectListItem { Value = "2", Text = "item 2" },
        new SelectListItem { Value = "3", Text = "item 3" },
    };
    return View(model);
}

and finally the strongly typed view in which you will display the dropdown list:

@model MyViewModel
@Html.DropDownListFor(x => x.SelectedValue, Model.Values)

UPDATE:

According to your updated question you are have an IEnumerable<SelectListItem> property on your view model to which you are trying to assign a value of type IEnumerable<string> which obviously is impossible. You could convert this to an IEnumerable<SelectListItem> like this:

var domains = FetchAllDomains().Select(d => new SelectListItem
{
    Value = d.DomainName,
    Text = d.DomainName
});
return new EmailModel { DomainList = domains };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...