首页 > 解决方案 > 使用 MySql 从 ASP.Net MVC 中的 ViewModel 填充(绑定)DropDownList

问题描述

我试图搜索帖子,也没有任何结果,也许我没有使用正确的词。

我需要一个解决方案,MVCDropDownList使用Model classHtml.DropDownListForHelper 方法从数据库中填充。

我在调试时没有错误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 2019返回错误

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

标签: c#asp.netasp.net-mvcdrop-down-menu

解决方案


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

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

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


推荐阅读