如何用我的IEnumerable <model>值ASP.NET MVC填充/填充DropDown

时间:2017-07-06 13:16:14

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

我尝试从我的模型中设置下拉列表值,因为它是我需要在下拉列表中看到的值 但我面临一个错误:

  

方法的类型参数   “System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,   System.Linq.Expressions.Expression&gt;中   System.Collections.Generic.IEnumerable,   字符串)'无法从使用中推断出来。尝试指定类型   明确的论点。

这是我的代码:

session_start();
$data='';
while($rows=mysql_fetch_array($result)){
    $user['userid'] = $rows['id'];
    $user['firstname'] = $rows['firstname'];
    $user['lastname'] = $rows['lastname'];
    $data[]=$user;

}
$_SESSION['Data'] = $data;

控制器操作:

@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>

                    }

                }
            }           
        }               
    }               
}

也许我可以删除public ActionResult Settings() { List<Language> activeL = LanguageController.GetAll(); ViewBag.Languagess = activeLanguages; return View(activeL); } ,因此我可以ViewBag.Languagess使用(Model, "Id", "Name")

6 个答案:

答案 0 :(得分:2)

你需要写这样的HTML

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

在您的控制器中

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

   return View();

}

答案 1 :(得分:1)

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" })

答案 2 :(得分:1)

由于您已经请求如何使用强类型模型而不是ViewBag ....

现在,您的视图需要IEnumerable<Language>类型的模型。将此更改为包含页面所需值的新类。

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

然后在你的控制器中:

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

更改您的视图以要求使用LanguagesViewModel:

@model LanguagesViewModel

现在您不会使用动态视图包。

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

答案 3 :(得分:0)

我认为你的模型可能是其他类型的设置。

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>

                    }

                }
            }           
        }               
    }               
}

控制器

    [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.

    }

答案 4 :(得分:0)

<强>模型

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

<强>控制器

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

查看

@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>

                    }

                }
            }           
        }               
    }               
} 

答案 5 :(得分:-1)

它会像这个例子一样工作:

@{
   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 --")