473,466 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Sep 16 '08 #1
1 4252
<%= Html.DropDownList(null, "Targets") %>
change to
<%= Html.DropDownList("", "Targets") %.

"shapper" <md*****@gmail.com写入消息
news:05**********************************@b1g2000h sg.googlegroups.com...
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
Sep 17 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
2
by: Leon Shaw | last post by:
have two Dropdownlist and one which is containing States and one containing Cities. I know how to do all the little databinding task in visual studios interface as well as set the relationship...
6
by: VB Programmer | last post by:
I have an itemtemplate in a datagrid. I'm trying to set it's value based on data. Having no trouble with the textbox. But, how do I do the same thing for a dropdownlist???? <ItemTemplate>...
4
by: wolfgang wagner | last post by:
hi all! after successfully integrating a dropdownlist in my datagrid i have another problem: i cannot set the selected index of the dropdownlistbox. here is my code: hardware.aspx...
0
by: Evan Camilleri | last post by:
I managed to do a DropDownList in Datagrid on Edit (normally, the templates puts a label). My problem is that I cannot initialize it when I do the edit. I am doing the following ' Select the...
19
by: Bernie Yaeger | last post by:
Everyone misses the point on this - what we need is a combobox autocomplete that is a dropdownlist only. When in dropdown mode, you can enter text - making that autocomplete is trivial. But when...
2
by: Jeff User | last post by:
Hi C# .NET1.1 I have read Visual Studio help and several articles on the web. Obviously this is a common topic. However, I see this code over and over yet I am stuck with the FindControl method...
15
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
1
by: Vagabond Software | last post by:
I'm using a DropDownList in an EditItemTemplate column. This DropDownList will only ever have two dates in it; Now and the default DateTime value (1/1/1900 12:00:00 AM). Somewhere, somehow, the...
3
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.