When I have trying to showing my dropdownlist values in my view page using asp.net mvc razor.My code as given below
In my Class
public static IEnumerable<SelectListItem> RoleList() { var user = CommonService.UserManager.CustomeUserManager.GetUserDetails(); Entities _ent = new Entities (); var data = (from a in _ent.AspNetRoles
select new { RoleName = a.Name, RoleId = a.Name, }).ToList(); return new SelectList(data, "Name", "Name"); }
In my Class
public class StaffInfo
{
public string RoleName{get;set;}
}
In my View page
<div class="col-sm-3">
<label>Role <span class="text-danger">*</span></label>
@Html.DropDownList("RoleName",
SMS.Utilities.CommonUtilities.RoleList(),new { @class = "form-control" })
</div>
This type of error has been occured:
DataBinding: '<>f__AnonymousType56`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Name'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: DataBinding: '<>f__AnonymousType56`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Name'.
Answer
Option 1.
Change in your RoleList function.In the above function the role name is assign RoleName variable and get Name only which is not variable name in the function.
public static IEnumerable<SelectListItem> RoleList()
{
SMSEntities _ent = new SMSEntities();
var data = (from a in _ent.AspNetRoles
where a.Name!="SuperAdmin"
select new
{
RoleName = a.Name,
}).ToList();
return new SelectList(data, "RoleName", "RoleName");
}
Comments