简体   繁体   中英

How can I fill / populate DropDown with my IEnumerable<Model> value ASP.NET MVC

I tried to set my dropdown list values from my model as it is the value I need to see in the dropdown But I'm facing an error:

The type arguments for method 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable, string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

And here is my code:

@model IEnumerable<Language>
@using Web.Helpers
@{
    ViewBag.Title = "";
}
<div class="">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="x_panel">
                <div class="x_title">
                    <h2><i class="fa fa-filter"></i> </h2>
                    <div class="clearfix"></div>
                </div>
                <div class="x_content">
                    <br />
                    @using (Html.BeginForm("Settings","Language", FormMethod.Post, new { @class = "form-horizontal form-label-left" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-md-12 col-sm-12">
                            <div class="col-md-12 col-sm-12">
                                @Html.DropDownListFor(WHATTOWRITEHERE, new SelectList(ViewBag.Languagess, "Id", "Name"), new { @class = "form-control" })
                            </div>
                        </div>

                    }

                }
            }           
        }               
    }               
}

Controller action:

public ActionResult Settings()
{
   List<Language> activeL = LanguageController.GetAll();
   ViewBag.Languagess = activeLanguages;

   return View(activeL);
}

Maybe I could delete ViewBag.Languagess at all, so I can use (Model, "Id", "Name") there instad of (ViewBag.Languagess, "Id", "Name")

You need to write html like this

@Html.DropDownListFor(WHATEVER, new SelectList(ViewBag.languages, "Id", "Name"), new { @class = "form-control" })

And in your controller

public ActionResult Settings()
{
   List<Language> activeL = LanguageController.GetAll();
   ViewBag.Languagess = activeL;

   return View();

}

public ActionResult Settings()
{
   List<Language> activeL = LanguageController.GetAll();

   ViewBag.Languages = activeLanguages.Select(item => new SelectListItem{
       Text = item.Name,
       Value = item.Id
    });

   return View(activeL);
}

@Html.DropDownListFor(m => m.LanguageId, ViewBag.Languages, new { @class = "form-control" })

Since you've requested how to do this with a strongly typed model instead of a ViewBag....

Right now your view requires a model of type IEnumerable<Language> . Change this to be a new class that holds all values your page needs.

public class LanguagesViewModel
{
    List<Language> ActiveLanguages { get; set; }
}

Then in your controller:

var model = new LanguagesViewModel();
model.ActiveLanguages = activeLanguages;
return View(model);

Change your View to require a LanguagesViewModel:

@model LanguagesViewModel

Now you won't be working with the dynamic view bag.

@Html.DropDownListFor(m => m.LanguageId, Model.ActiveLanguages, new { @class = "form-control" })

I thing your model could been an other type like Settings.

public class SettingsModel
{
    public int LanguageId { get; set; }
}

public class Language
{
    public int Id { get; set; }
    public string Name { get; set; }
}

.cshtml

@model SettingsModel
@using Web.Helpers
@{
    ViewBag.Title = "";
}
<div class="">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="x_panel">
                <div class="x_title">
                    <h2><i class="fa fa-filter"></i> </h2>
                    <div class="clearfix"></div>
                </div>
                <div class="x_content">
                    <br />
                    @using (Html.BeginForm("Settings","Language", FormMethod.Post, new { @class = "form-horizontal form-label-left" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-md-12 col-sm-12">
                            <div class="col-md-12 col-sm-12">
                                @Html.DropDownListFor(model => model.LanguageId, new SelectList(ViewBag.Languagess, "Id", "Name"), new { @class = "form-control" })
                            </div>
                        </div>

                    }

                }
            }           
        }               
    }               
}

controller

    [HttpGet]
    public ActionResult Language()
    {
        List<Language> activeL = LanguageController.GetAll();
        ViewBag.Languagess = activeLanguages;
        return View();
    }

   [HttpPost]
    public ActionResult Language(SettingsModel settings)
    {
        Language selectedLanguage = LanguageController.GetAll().Where(l => l.Id == settings.LanguageId).SingleOrDefault();
        //...   your post operations.

    }

model

   public class LanguageViewModel
    {
        public int LanguageId { get; set; }
        public IEnumerable<Language> ActiveLanguages{get; set;}
    }

    public class Language
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Controller

   [HttpGet]
   public ActionResult Language()
    {
       var languageViewModel = new LanguageViewModel
        {
          ActiveLanguages = LanguageController.GetAll();
        }
         return View(languageViewModel);
     }

View

@model LanguageViewModel
@using Web.Helpers
@{
    ViewBag.Title = "";
}
<div class="">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="x_panel">
                <div class="x_title">
                    <h2><i class="fa fa-filter"></i> </h2>
                    <div class="clearfix"></div>
                </div>
                <div class="x_content">
                    <br />
                    @using (Html.BeginForm("Settings","Language", FormMethod.Post, new { @class = "form-horizontal form-label-left" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-md-12 col-sm-12">
                            <div class="col-md-12 col-sm-12">
                                @Html.DropDownListFor(model => model.LanguageId, new SelectList(Model.ActiveLanguages, "Id", "Name"), new { @class = "form-control" })
                            </div>
                        </div>

                    }

                }
            }           
        }               
    }               
} 

It would work like in this example:

@{
   List<SelectListItem> listItems= new List<SelectListItem>();
   listItems.Add(new SelectListItem
        {
          Text = "Exemplo1",
          Value = "Exemplo1"
        });
   listItems.Add(new SelectListItem
        {
            Text = "Exemplo2",
            Value = "Exemplo2",
            Selected = true
        });
   listItems.Add(new SelectListItem
        {
            Text = "Exemplo3",
            Value = "Exemplo3"
        });
}

@Html.DropDownListFor(model => model.tipo, listItems, "-- Select Status --")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM