使用 MySql 在 ASP.Net MVC 中从 ViewModel 填充(绑定)DropDownList

Populate (Bind) DropDownList from ViewModel in ASP.Net MVC using MySql

我尝试搜索帖子,也没有任何结果,可能是我用词不对。

我需要 MVC 中的解决方案,因为 DropDownList 将使用 Model classHtml.DropDownListFor 辅助方法从数据库中填充。

我在 Visual Studio 2019 调试时没有错误,但是 DropDownList 是空的。

请帮帮我。

下面是我的代码

型号

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

控制器

namespace InGE.Controllers
{
    public class HomeController : Controller
    {
        private static List<SelectListItem> PopulateFruits()
        {
            string sql;

            List<SelectListItem> items = new List<SelectListItem>();

            string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;

            using (MySqlConnection con = new MySqlConnection(constr))
            {
                sql = @String.Format("SELECT * FROM `dotable`; ");

                using (MySqlCommand cmd = new MySqlCommand(sql))
                {
                    cmd.Connection = con;
                    con.Open();

                    using (MySqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            items.Add(new SelectListItem
                            {
                                Text = sdr["sName"].ToString(),
                                Value = sdr["sCode"].ToString()
                            });
                        }
                    }
                    con.Close();
                }
            }

            return items;
        }

        [HttpPost]
        public ActionResult Index(PersonModel person)
        {
            string sCl = person.CL;
            string sTicket = person.Ticket;       
            string sName = person.Namex;
            string sCode = person.Codex;

            PersonModel fruit = new PersonModel();
            fruit.Fruits = PopulateFruits();

            return View();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

查看

@Html.DropDownListFor(m => m.Fruits, Model.Fruits, "Please select")

更新

现在调试Visual Studio 2019return错误

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

您应该在 Index GET 方法中填充模型,然后 return 将模型作为参数传递给视图。

[HttpGet]
public ActionResult Index()
{
    PersonModel fruit = new PersonModel();
    fruit.Fruits = PopulateFruits();
    return View(fruit);
}

这是当您导航到“索引”页面时将调用的方法,因此您在此处放入模型的任何内容都会成为视图中的模型。
当您的用户在 Form 标记内按下某个按钮或 link 以 post 返回视图中编辑的信息时,将调用 HttpPost 方法。