DropDownList for Dropdown

Original Link: https://my.oschina.net/Andyahui/blog/611603

In View Model<Vo>we need to use IEnumerable to convert all data from other lists into drop-down lists.Below is a description of the actual writing in the project.

1: Implement the writing of drop-down attribute list

Use SelectListItem to automatically populate DropDownList to form a drop-down box.

DropDownList is a feature tag that we want to use to make data drop-down on the foreground page, but only if we set it in Action and assign values to Value and Text.

The following is how attributes are written, which is the IEnumerable<>interface type

public class CreatCustomerView
    {
        public CreatCustomerView()
        {
            this.Schools = new List<SelectListItem>();
        }
        /// <summary>
        ///Foreign keys
        /// </summary>
        [Display(Name = "School"), Required(ErrorMessage = "Cannot choose to be empty")]
        public Guid SchoolId { get; set; }
        /// <summary>
        ///Navigation properties of schools
        /// </summary>
        public IEnumerable<SelectListItem> Schools { get; set; }
        /// <summary>
        /// OpenId: The only representation bound to WeChat, obtained from WeChat
        /// </summary>
        public string OpenId { get; set; }
    }

Writing this means that you want to put its Schools in a collection and write the SelectListItem on it when it is initialized.

SelectListItem represents the selection of an instance of System.Web.Mvc.SelectList class.Settings will be made here in Action.

IEnumerable <T> interface type: This is necessary for Foreach traversal because all collections and data inherit from this interface and support simple iteration of non-generic methods, which are set accessors.Defines an extension method for traversing, filtering, sorting, searching, and so on, elements in a data set.

Two: Writing in Action

This is where the Value and Text are assigned.

public ActionResult ChooseSchool()
        {
             var entity = new CreatCustomerView();
             entity.Schools = _schoolService.GetAll()
                     .Where(x => x.Id != Guid.Empty)
                     .Select(x => new SelectListItem
                      {
                          Text = x.Name,
                          Value = x.Id.ToString()
                       }).ToList();
               return View(entity);
        }

First, the data in the database table is taken out by the GetALL method, assigned by Select, converted to ToList(), and passed to the view.This is where you assign values and prepare for Foreach on the foreground page in the future.

Three: Writing in View View

Inside the view is achieved through the DropDownList in HtmlHelper, but the reality of DropDownList is achieved through the following three steps.(

That's what happened in the first two steps, and here's the code in View.

@{
    ViewBag.Title = "Choose a school";
    Layout = "~/Views/Shared/_LayoutNew.cshtml";
}
@using System.Runtime.InteropServices
@model Express.Weixin.Web.Models.CreatCustomerView
<div class="header"><a href="@Url.Action("Index","Member")" class="btn btn-link btn-xs pull-left">Return</a>Choose a school</div>
@using (Html.BeginForm(null, null, FormMethod.Post))
{
    <input type="hidden" value="@ViewBag.OpenId" name="OpenId" />
    <div class="col-sm-5 center" style="margin: auto;position: absolute; top: 0; left: 0; bottom: 0; right: 0; ">
        <br/><br/><br />
        @Html.LabelFor(x => x.SchoolId)
        @Html.DropDownListFor(x => x.SchoolId, Model.Schools, new { @class = "form-control" })
        @Html.ValidationMessageFor(x => x.SchoolId)
        <br/>
        <input type="submit" class="btn btn-success btn-sm btn-block" value="Choose a school"/>
    </div>
}

Through the @Html.DropDownListFor (x => x.SchoolId, Model.Schools, new { @class = "form-control"}) to achieve the drop-down result.

Four: Display results

Attachment: DropDownList Knowledge Reference http://www.cnblogs.com/kirinboy/archive/2009/10/28/use-dropdownlist-in-asp-net-mvc.html

Reprinted at: https://my.oschina.net/Andyahui/blog/611603

Tags: Attribute Database

Posted on Sat, 07 Sep 2019 11:29:33 -0700 by rich86