C# 使用MySql从ASP.NETMVC中的ViewModel填充(绑定)DropDownList
我试着搜索帖子,也没有任何结果,也许我没有使用正确的词 我需要在C# 使用MySql从ASP.NETMVC中的ViewModel填充(绑定)DropDownList,c#,asp.net,asp.net-mvc,drop-down-menu,C#,Asp.net,Asp.net Mvc,Drop Down Menu,我试着搜索帖子,也没有任何结果,也许我没有使用正确的词 我需要在MVC中为DropDownList提供一个解决方案,它将使用Model class和Html.DropDownListForHelper方法从数据库中填充 我在Visual Studio 2019的调试中没有错误,但下拉列表是空的 请帮帮我 下面是我的代码 型号 namespace InsGE.Models { public class PersonModel { public List<Sel
MVC
中为DropDownList
提供一个解决方案,它将使用Model class
和Html.DropDownListFor
Helper方法从数据库中填充
我在Visual Studio 2019的调试中没有错误,但下拉列表是空的
请帮帮我
下面是我的代码
型号
namespace InsGE.Models
{
public class PersonModel
{
public List<SelectListItem> Fruits { get; set; }
public string Namex { get; set; }
public string Codex { get; set; }
[Required]
[Display(Name = "CL")]
public string CL { get; set; }
[Required]
[Display(Name = "Ticket")]
public string Ticket { get; set; }
}
}
更新
现在,Visual Studio 2019的调试返回错误
System.Web.Mvc.WebViewPage<TModel>.Model.get
Object reference not set to an instance of an object error
App_Web_sxdtrbqy
您应该在Index GET方法中填充模型,然后将模型返回给作为其参数传递的视图
[HttpGet]
public ActionResult Index()
{
PersonModel fruit = new PersonModel();
fruit.Fruits = PopulateFruits();
return View(fruit);
}
这是当您导航到索引页面时将调用的方法,因此您在此处模型中放置的任何内容都将成为视图中的模型。当用户按下表单标记内的某个按钮或链接以发回视图中编辑的信息时,将调用HttpPost方法
System.Web.Mvc.WebViewPage<TModel>.Model.get
Object reference not set to an instance of an object error
App_Web_sxdtrbqy
@Html.DropDownListFor(m => m.Fruits, Model.Fruits, "Please select")
[HttpGet]
public ActionResult Index()
{
PersonModel fruit = new PersonModel();
fruit.Fruits = PopulateFruits();
return View(fruit);
}