Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want to show a dropdownList on a page using Entity Framework in my MVC app, but i am just stuck here to do this using using HTML Helper. so if anyone having knowledge of entity framework, help me...

my dataContext partial class is Entities, in which an entity named MemberInfo have some fields including MemberID & MemberName, so my dropdownList should show the field MemberName & behind this the value should be MemberID,

the code i tried yet--->

@Html.DropDownListFor(Model => Model.MemberID, MemberInfo)

in controller i am returning the Model--->

var MemberNameList = FinanceDBContext.MemberInfoes.Select(x => new { x.MemberID, x.Name });
return View(MemberNameList);  

but its not working (errors).

share|improve this question
 
What are the errors? Is your view strongly-typed? There are lots of examples on SO of how to achieve this, have you searched? –  Belogix May 20 at 9:01
 
the MemberNameList i am returning to the view, how to use this to show the dropdownList. the first line of code above is not valid (second arg shd b an enumerable type). –  Ashok Damani May 20 at 9:06
 
can u rewrite this line with right syntex, i'v googled alot, but not found any suitable answer ! –  Ashok Damani May 20 at 9:07
 
I've put full example of showing list and getting who was selected. –  Belogix May 20 at 9:14
add comment

1 Answer

up vote 0 down vote accepted

You need to pass in all of your objects as the "model". Best practice is to use a ViewModel which will contain the list of data and a property to store the selected item.

ViewModel

public class MyViewModel
{
    // The drop-down list and variable to get selection
    public List<Member> Members { get; set; }
    public int SelectedMemberId { get; set; }
}

Controller

[HttpGet]
public ActionResult Index()
{
    var viewModel = new MyViewModel();
    viewModel.Members = FinanceDBContext.MemberInfoes.ToList();
    return View(viewModel);
}

[HttpPost]
public ActionResult Index(MyViewModel viewModel)
{
    string debug = string.Format("You selected member: {0}", viewModel.SelectedMemberId);
    return View(viewModel);
}

Finally, in your view (these lines need to be inside a BeginForm { ... } and ensure your View is strongly typed to MyViewModel

@Html.DropDownList("SelectedMemberId", new SelectList(Model.Members, "MemberID", "Name"))
<input type="submit" value="Save etc..." />

In this example you could put a break-point on the HttpPost action and check the debug string to check the correct Member is returned and proceed as required.

share|improve this answer
 
The type arguments for method 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor<TModel,TProperty>(System.W‌​eb.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. –  Ashok Damani May 20 at 9:20
 
ur code contains the above error. –  Ashok Damani May 20 at 9:20
 
That is because your view should be strongly typed to "MyViewModel" not "SelectListItem" - I've added that note in instructions. –  Belogix May 20 at 9:29
 
nope !!! this is bcoz u'v wrote DropDownListFor instead of DropDownList. Now its working, thanx for ur answer. –  Ashok Damani May 20 at 9:33
 
yeah sure, can u help me in one more thing. i want to show selected member name in a label Text (whenever user selects a option, that label text shd change as selected value (MemberID) ) –  Ashok Damani May 20 at 9:50
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.