4

Hi i want to fill a dropdown list in my view page my code is

public class MemberBasicData
{
    public int Id { get; set; }
    public string Mem_NA { get; set; }
    public string Mem_Occ { get; set; }
}

//controller

  public ActionResult Register()
    {
        var users = new Member().GetAllMembers();
        ViewBag.Users = users;   
        return View();
    }

//View

 @model IEnumerable<....Members.Models.MemberBasicData>
 @Html.DropDownListFor(model => model.Mem_Na, (SelectList)ViewBag.Users, "--Select Users--")

i want to add Mem_NA to dropdownlist, Viewbag contains all the user details. Problem is in View page. An error shows in model.Mem_Na. Please help me to solve this.

2
  • 3
    and what is the error? Commented Aug 26, 2013 at 6:28
  • @No One:could contain a definition for Mem_NA Commented Aug 26, 2013 at 6:30

3 Answers 3

8

Please, change your code

from

@model IEnumerable<....Members.Models.MemberBasicData>
@Html.DropDownListFor(model => model.Mem_Na, (SelectList)ViewBag.Users, "--Select Users--")

to

@model ....Members.Models.MemberBasicData
@Html.DropDownListFor(model => model.Id,new SelectList(ViewBag.Users,"Id","Mem_NA"), "--Select Users--")

It works for me everytime

Sign up to request clarification or add additional context in comments.

Comments

1

Your model is an IEnumerable of MemberBasicData, not an instance of MemberBasicData. This means you cannot access properties of MemberBasicData directly, as the property doesn't exist against an IEnumerable.

From what little I can see from your code I imagine you want your model to be an instance, not a list.

Comments

0

Misspelling? You have spelled Mem_Na in your view and Mem_NA in your model

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.