📜 ⬆️ ⬇️

DropDownList, Set “value” for the default option in MVC 4

Hello. For more than two years, he created websites for PCPs and everything was fine. But one day I woke up and realized that pkhp is good, but not that. Then he looked at how to write web applications for a long time, compared, analyzed. As a result, I stopped at C # and ASP.NET MVC 4. Even after Zend Framework 2 - MVC 4 is just something: easy, beautiful, simple, fast, little code.

But I ran into a problem that I did not find a solution to in Google. How to display a drop-down list and set the default value. I think for the "experienced" - it will not be a problem. This is something that once "drive through" and never again remember that there were problems with this. but it is quite difficult to get around for a beginner. To be honest, at first it was just hard to get a drop-down list. Although the ASP.NET MVC manuals are an order of magnitude better than the “Quick Start” for any php framework.

This manual assumes that you are already operating with the knowledge gained from reading these articles: Entity Framework in an ASP.NET MVC application . Or these: ASP.NET MVC 4 Tutorials
')
Let's start with the withdrawal list.
1) Model. There are no annotations and links to simplify the example.

using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace zf2.Models { public class NewsM { public int NewsMID { get; set; } public int ParentID { get; set; } public string Title { get; set; } public string AddTitle { get; set; } public string Description { get; set; } public string Content { get; set; } public DateTime ModDate { get; set; } } } 


2) Controller. Right-click on the controller folder — create a controller and select a Template: “MVC-controller with read, write and view actions using the Entity Framework”, Model class “NewsM”, Data context class: which one.

And add the method to it:
  //   private void PopulateDepartmentsDropDownList(object selectedDepartment = null) { var departmentsQuery = (from d in db.NewsMs orderby d.NewsMID select d).ToList<NewsM>(); //         ,     Contoso University //NewsMID = 0 -    value //Title = "Add to root" - text departmentsQuery.Add(new NewsM { NewsMID = 0, Title = "Add to root" }); ViewBag.ParentID = new SelectList(departmentsQuery, "NewsMID", "Title", selectedDepartment); // departmentsQuery - ,      DropDownList //"NewsMID" -      ,      value //"Title" -      ,      text (,     ) //selectedDepartment -  selected.     . } 

Next, we call the Create and Edit actions:
  // GET: /News/Create public ActionResult Create() { PopulateDepartmentsDropDownList(); return View(); } // GET: /News/Edit/5 public ActionResult Edit(int id = 0) { NewsM newsm = db.NewsMs.Find(id); if (newsm == null) { return HttpNotFound(); } //  newsm.ParentID -   selected . PopulateDepartmentsDropDownList(newsm.ParentID); return View(newsm); } 

3) In the view. Just add the following code to the right place.
Create.cshtml and Edit.cshtml
  <div class="editor-label"> @Html.LabelFor(model => model.ParentID) </div> <div class="editor-field"> @Html.DropDownList("ParentID") <!-- "ParentID"     ,       ViewBag.ParentID = new SelectList(...  PopulateDepartmentsDropDownList.   ,   "ParentID"     , ,   ...--> @Html.ValidationMessageFor(model => model.ParentID) </div> 


By the way, the model described at the beginning is a table for storing and building a dynamic menu of any nesting. She was also a separate case. I'll tell you in the next article.

Source: https://habr.com/ru/post/174455/


All Articles