ASP.NET MVC DropDownList ... What am I doing wrong?

S

shapper

Hello,

I am trying to display a DropDownList on an ASP.NET MVC view.

SlidePaper.Targets contains the list of items and
SlidePaper.Slide.Target contains the current value taken from the
database for the given record ...

This works but does not give the current value, only the list:
<%= Html.DropDownList(null, "Targets") %> .

Then do display the list and the current value I tried the following:
<%= Html.DropDownList(null, (string)ViewData["Target"] ??
ViewData.Model.SlidePaper.Slide.Target.ToString(),
(SelectList)ViewData["Targets"])%>

It gives an error:

Value cannot be null.
Parameter name: selectList
Use the "new" keyword to create an object instance

I don't know how to solve this. Could someone, please, help me out?

On my controller I have:

public ActionResult Create() {
SlideViewData viewData = new SlideViewData();
viewData.Targets = Asset.Targets();
viewData.SlidePaper.Slide.Target = "_blank";
return View("Create", viewData);
}

I am testing this on my Create view but as you can see I am defining
Targets and Target.

Asset.Targets is:

public static SelectList Targets() {
var targets = new Dictionary<string, string>();
targets.Add("Blank", "_blank");
targets.Add("Parent", "_parent");
targets.Add("Self", "_self");
targets.Add("Top", "_top");
var list = new SelectList(targets, "Value", "Key");
return list;
}

Thank You,

Miguel
 
G

Guest

<%= Html.DropDownList(null, "Targets") %>
change to
<%= Html.DropDownList("", "Targets") %> .
 
Joined
Aug 22, 2009
Messages
2
Reaction score
0
How to Bind MVC dropdownlist?

Hi, this article provides details about how to bind MVC dropdownlist?

altafkhatri.com/Technical/How_to_bind_IList_with_MVC_Dropdownlist_box
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top