473,804 Members | 3,570 Online
Bytes | Software Development & Data Engineering Community
+ 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.Targ ets contains the list of items and
SlidePaper.Slid e.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.DropDownLi st(null, "Targets") %.

Then do display the list and the current value I tried the following:
<%= Html.DropDownLi st(null, (string)ViewDat a["Target"] ??
ViewData.Model. SlidePaper.Slid e.Target.ToStri ng(),
(SelectList)Vie wData["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.Target s = Asset.Targets() ;
viewData.SlideP aper.Slide.Targ et = "_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<stri ng, string>();
targets.Add("Bl ank", "_blank");
targets.Add("Pa rent", "_parent");
targets.Add("Se lf", "_self");
targets.Add("To p", "_top");
var list = new SelectList(targ ets, "Value", "Key");
return list;
}

Thank You,

Miguel
Sep 16 '08 #1
1 4269
<%= Html.DropDownLi st(null, "Targets") %>
change to
<%= Html.DropDownLi st("", "Targets") %.

"shapper" <md*****@gmail. com写入消息
news:05******** *************** ***********@b1g 2000hsg.googleg roups.com...
Hello,

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

SlidePaper.Targ ets contains the list of items and
SlidePaper.Slid e.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.DropDownLi st(null, "Targets") %.

Then do display the list and the current value I tried the following:
<%= Html.DropDownLi st(null, (string)ViewDat a["Target"] ??
ViewData.Model. SlidePaper.Slid e.Target.ToStri ng(),
(SelectList)Vie wData["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.Target s = Asset.Targets() ;
viewData.SlideP aper.Slide.Targ et = "_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<stri ng, string>();
targets.Add("Bl ank", "_blank");
targets.Add("Pa rent", "_parent");
targets.Add("Se lf", "_self");
targets.Add("To p", "_top");
var list = new SelectList(targ ets, "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
9094
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 and the 2nd dropdown (spaces) is updated. However, 'spaces' no longer has a SelectedIndexChanged event. Where did it go to? If I change 'spaces' (2nd dropdown) before it gets updated by the 1st (sites), its SelectedIndexChanged is fired and the...
2
3554
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 (parent, child, etc.). In the code behind file in page_load event I fill the data adapter and databind the State dropdownlist. I also set the on_change event for the Cities dropdownlist after a state is selected. However, When I run this page in the...
6
1781
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> <asp:DropDownList id="ddlYesNo" runat="server" > <asp:ListItem Value="-">-</asp:ListItem> <asp:ListItem Value="Yes">Yes</asp:ListItem> <asp:ListItem Value="No">No</asp:ListItem> </asp:DropDownList>
4
2108
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
306
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 current row for 'edit mode' dg.EditItemIndex = e.Item.ItemIndex BindDataGrid()
19
18358
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 you look at combobox controls from years ago, they would autocomplete in dropdownlist mode. And if you look at intellisense, this is exactly what happens, so obviously the .net framework can do it. Any help would be much appreciated. Bernie...
2
3108
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 when trying to locate my DropDownList so that I can populate it. It doesn't find the control Here is what I have: asp.net:
15
3134
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 OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
1
1876
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 DropDownList is interpreting the default DateTime value as a string value "1/1/0001 12:00:00 AM". DateTime.Parse in turn converts that string value to #12:00:00 AM# with no date values. What am I doing wrong here? Here is some relevant code...
3
2372
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 Edit. I have been trying to hunt down a viable soltion to this, seems that many people out there have similar issues and the ...SelectedItem.Value or SelectedValue is not returning what is actually being selected by the end user. So the simple question...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4291
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 we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.