Home > front end >  ASP.NET.MVC drop-down box binding
ASP.NET.MVC drop-down box binding

Time:12-02

In MVC binding drop-down box, I generally ViewBag methods,
ViewBag detailing ViewBag is a dynamic type variable (dynamic), it is a new feature of c # 4.0 introduced, variable type can be in luck during parsing, ViewBag is basically the codebehind (categories.aspx.cs) packaging, also be used to transfer value from the Controller to the View, ViewBag is effective in the current request, only after the redirection (redirection), ViewBag stored in the variable value will be null because the ViewBag is a dynamic type, so we get its value, do not need to type conversion,
The first step: first, the data query in MVC controller, then the query of data encapsulation into the Viewbag

The database images:

Query the database as shown in figure:

Step 2: put the Viewbag data binding to the view
As shown in figure:

Then find a drop-down box to add a foreach statement cycle bound data
As shown in figure:

Note: ViewBag its stored data object life cycle, in other words, ViewBag stored value only in the life cycle from the Controller to the View, and the data is not in from the Controller to the View in the ViewBag effective, between different Action, or jump from one page to another page (Controller to Controller) is invalid,
Complete the rendering is as follows:

  • Related