. Net mvc ------ drop-down list DropDownList control ------ bind data, mvc drop-down list

Source: Internet
Author: User

. Net mvc ------ drop-down list DropDownList control ------ bind data, mvc drop-down list

The drop-down list uses gender as an example.

You can bind the file to it. It can be displayed, but values can be transferred in some places, and errors may occur in some places. If you have any great idea, please advise ....

The error is as follows:

The ViewData item with the key "sex" belongs to the type "YTgoShopping. Utilities. sex", but must belong to the type "IEnumerable <SelectListItem> ".

The ViewData item with the key "sex" belongs to the type "Int32", but it must belong to the type "IEnumerable <SelectListItem> ".

--------------------------------------

Run ctrl + K + D if the code replication format is messy.

The code is not fully written. If there is a ripple line, please parse-Reference

---------------------------------------

View code

// Expression, a collection of objects, an object ----- the first Null Value

@ Html. DropDownListFor (model => model. sex, ViewBag. sex as IEnumerable <SelectListItem>, "select gender ")

@ Html. DropDownListFor (model => model. sex, ViewBag. sex as SelectList, "select gender ")

// Put the form name back. It is generally a model field, a set of objects, and an object. The first value is null.

@ Html. DropDownList ("sex", ViewBag. sex as IEnumerable <SelectListItem>, "select gender ")

@ Html. DropDownList ("sex", (SelectList) ViewData ["sex"], "select gender ")

@ Html. DropDownList ("sex", "select gender ")

 

 

Controller code

 

Public ActionResult EditInfo (int id)
{
Admin Admins = db. Admins. Where (a => a. AdminID = id). FirstOrDefault ();
ViewBag. sex = new SelectList (EnumHelper. GetSelectList <sex> (), "Value", "Text", Admins. sex); // This sentence can be changed
Return View (Admins );
}

The data source is enumerated

// The parameters are data set, data value, data text, and selected values in sequence-default items

ViewBag. sex = new SelectList (EnumHelper. GetSelectList <sex> (), "Value", "Text", Admins. sex );

ViewData ["sex"] = new SelectList (EnumHelper. GetSelectList <sex> (), "Value", "Text", Admins. sex );

// The parameters are set in sequence. The value of the selected item is the default item.

ViewBag. sex = new SelectList (Enum. GetValues (typeof (sex )),"");

If the data source is

ViewBag. list = new SelectList (db. sex, "Id", "Name ","");

 

Enumeration code

 

Public enum sex
{
Female = 0,
Male = 1,
Others = 2,
}

Or

Public enum sex
{
/// <Summary>
/// Female
/// </Summary>
[SelectDisplayName ("")]
Female = 0,

/// <Summary>
/// Male
/// </Summary>
[SelectDisplayName ("male")]
Male = 1,

/// <Summary>
/// Others
/// </Summary>
[SelectDisplayName ("other")]
Other = 2
}

 

To obtain Chinese characters using the second enumeration, use the following method:

Retrieve enumeration comments

Namespace Common
{

/// <Summary>
/// Custom annotation attributes
/// </Summary>
Public class SelectDisplayNameAttribute: Attribute
{
Private string _ diaplayName;
Public string DisplayName
{
Get
{
Return _ diaplayName;
}
}
Public SelectDisplayNameAttribute (string displayName)
{
_ DiaplayName = displayName;
}

}

Public class EnumHelper
{
/// <Summary>
/// Get the content obtained from Custom Attributes
/// </Summary>
/// <Param name = "obj"> </param>
/// <Returns> </returns>
Private static string GetEnumDescription (Object obj)
{

// Obtain the enumerated type of the enumerated object
Type type = obj. GetType ();
// Obtain all attributes of this enumeration type through reflection
FieldInfo [] fieldInfos = type. GetFields ();
Foreach (FieldInfo field in fieldInfos)
{
// Skip directly if it is not the obj Parameter
If (field. Name! = Obj. ToString ())
{
Continue;
}
// Retrieve the custom attribute of the obj Parameter
If (field. IsDefined (typeof (SelectDisplayNameAttribute), true ))
{
Return (field. GetCustomAttributes (typeof (SelectDisplayNameAttribute), true) [0] as SelectDisplayNameAttribute). DisplayName;
}

}
Return obj. ToString ();
}

/// <Summary>
/// Combine enumerated values and custom attributes into a List <SelectListItem/>
/// </Summary>
/// <Param name = "enumType"> </param>
/// <Returns> </returns>
Public static List <SelectListItem> GetSelectList <T> (object defaultvalue = null)
{
Var enumType = typeof (T );
List <SelectListItem> selectList = new List <SelectListItem> ();
Foreach (var obj in Enum. GetValues (enumType ))
{
// Note that Value = obj. ToString () is required ()
// The cause is Value = (int) (obj. toString (), so the enumerated value is the corresponding int value, in @ Html. the default value cannot be displayed in DropDownListFor. the cause is unknown.
SelectList. Add (new SelectListItem {Text = GetEnumDescription (obj), Value = obj. ToString ()});
}
Return selectList;
}
Public static string GetEnumName (Object obj)
{
Return GetEnumDescription (obj );
}
}
}

 

 

 

Welcome to revise me !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.