Can anyone using my code below, please show me how to add a dropdownlistfor as i just cannot get it to work.
Thanks
George
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Web.Caching;
using Web.Domain.Models.Currency.Entities;
using Web.Domain.Models.Currency.Abstract;
namespace Web.Domain.Models.Currency.Concrete
{
public class GetCurrencyDataFromDB : IGetCurrencyDataFromDB
{
private string dbConn;
public GetCurrencyDataFromDB()
{
dbConn = ConfigurationManager.ConnectionStrings["LocalSQLServer"].ConnectionString;
}
public List<GetCurrencyDataFieldsFromDB> GetCurrencyDatabaseFields()
{
string spName = "dbo.atSP_GetCountryCurrencies";
SqlConnection cn = new SqlConnection(dbConn);
SqlCommand cmd = new SqlCommand(spName, cn);
cmd.CommandType = CommandType.StoredProcedure;
List<GetCurrencyDataFieldsFromDB> CurrencyData = new List<GetCurrencyDataFieldsFromDB>();
try
{
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult);
while (rdr.Read())
{
GetCurrencyDataFieldsFromDB GetData = new GetCurrencyDataFieldsFromDB(
(string)rdr["CurrencyID"],
(string)rdr["Country"]);
CurrencyData.Add(GetData);
}
rdr.Close();
return CurrencyData;
}
catch (SqlException)
{
throw new ApplicationException("Error");
}
finally
{
if (cn.State == ConnectionState.Open)
{
cmd.Dispose();
cn.Close();
cn.Dispose();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Domain.Models.Currency.Entities;
namespace Web.Domain.Models.Currency.Abstract
{
public interface IGetCurrencyDataFromDB
{
List<GetCurrencyDataFieldsFromDB> GetCurrencyDatabaseFields();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Web.Domain.Models.Currency.Entities
{
public class GetCurrencyDataFieldsFromDB
{
private string dboCurrencyID;
private string dboCountry;
public string DboCurrencyID
{
get {return dboCurrencyID;}
set { dboCurrencyID = value; }
}
public string DboCountry
{
get { return dboCountry; }
set { dboCountry = value; }
}
public GetCurrencyDataFieldsFromDB(string dboCurrencyID, string dboCountry)
{
this.dboCurrencyID = dboCurrencyID;
this.dboCountry = dboCountry;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Domain.Models.Currency.Abstract;
using Web.Domain.Models.Currency.Concrete;
using System.Collections;
namespace Web.UI.Controllers
{
public class HomeController : Controller
{
private IGetCurrencyDataFromDB IWR;
public HomeController()
: this(new GetCurrencyDataFromDB())
{
}
public HomeController(IGetCurrencyDataFromDB repository)
{
IWR = repository;
}
public ActionResult Index()
{
var ws = IWR.GetCurrencyDatabaseFields();
//ViewData["ddlCurrency"] = ws.AsEnumerable();
return View(ws);
}
}
}
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Web.Domain.Models.Currency.Entities.GetCurrencyDataFieldsFromDB>>" %>
<%: Html.DropDownList("ddlCurrency", new SelectList((IEnumerable)ViewData["ddlCurrency"]))%>
LearningASP_...
Member
242 Points
256 Posts
DropDownList
Mar 07, 2011 12:31 PM|LINK
Hi
Can anyone using my code below, please show me how to add a dropdownlistfor as i just cannot get it to work.
Thanks
George
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data.SqlClient; using System.Data; using System.Web.Caching; using Web.Domain.Models.Currency.Entities; using Web.Domain.Models.Currency.Abstract; namespace Web.Domain.Models.Currency.Concrete { public class GetCurrencyDataFromDB : IGetCurrencyDataFromDB { private string dbConn; public GetCurrencyDataFromDB() { dbConn = ConfigurationManager.ConnectionStrings["LocalSQLServer"].ConnectionString; } public List<GetCurrencyDataFieldsFromDB> GetCurrencyDatabaseFields() { string spName = "dbo.atSP_GetCountryCurrencies"; SqlConnection cn = new SqlConnection(dbConn); SqlCommand cmd = new SqlCommand(spName, cn); cmd.CommandType = CommandType.StoredProcedure; List<GetCurrencyDataFieldsFromDB> CurrencyData = new List<GetCurrencyDataFieldsFromDB>(); try { cn.Open(); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult); while (rdr.Read()) { GetCurrencyDataFieldsFromDB GetData = new GetCurrencyDataFieldsFromDB( (string)rdr["CurrencyID"], (string)rdr["Country"]); CurrencyData.Add(GetData); } rdr.Close(); return CurrencyData; } catch (SqlException) { throw new ApplicationException("Error"); } finally { if (cn.State == ConnectionState.Open) { cmd.Dispose(); cn.Close(); cn.Dispose(); } } } } } using System; using System.Collections.Generic; using System.Linq; using System.Web; using Web.Domain.Models.Currency.Entities; namespace Web.Domain.Models.Currency.Abstract { public interface IGetCurrencyDataFromDB { List<GetCurrencyDataFieldsFromDB> GetCurrencyDatabaseFields(); } } using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web.Domain.Models.Currency.Entities { public class GetCurrencyDataFieldsFromDB { private string dboCurrencyID; private string dboCountry; public string DboCurrencyID { get {return dboCurrencyID;} set { dboCurrencyID = value; } } public string DboCountry { get { return dboCountry; } set { dboCountry = value; } } public GetCurrencyDataFieldsFromDB(string dboCurrencyID, string dboCountry) { this.dboCurrencyID = dboCurrencyID; this.dboCountry = dboCountry; } } } using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Web.Domain.Models.Currency.Abstract; using Web.Domain.Models.Currency.Concrete; using System.Collections; namespace Web.UI.Controllers { public class HomeController : Controller { private IGetCurrencyDataFromDB IWR; public HomeController() : this(new GetCurrencyDataFromDB()) { } public HomeController(IGetCurrencyDataFromDB repository) { IWR = repository; } public ActionResult Index() { var ws = IWR.GetCurrencyDatabaseFields(); //ViewData["ddlCurrency"] = ws.AsEnumerable(); return View(ws); } } } <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Web.Domain.Models.Currency.Entities.GetCurrencyDataFieldsFromDB>>" %> <%: Html.DropDownList("ddlCurrency", new SelectList((IEnumerable)ViewData["ddlCurrency"]))%>raduenuca
All-Star
24675 Points
4250 Posts
Re: DropDownList
Mar 07, 2011 12:54 PM|LINK
Radu Enuca | Blog
LearningASP_...
Member
242 Points
256 Posts
Re: DropDownList
Mar 07, 2011 01:15 PM|LINK
Hi
No idea how to get that to work with my code
Thanks
George
raduenuca
All-Star
24675 Points
4250 Posts
Re: DropDownList
Mar 07, 2011 01:27 PM|LINK
It shows how to use a DropDownList
Radu Enuca | Blog
LearningASP_...
Member
242 Points
256 Posts
Re: DropDownList
Mar 07, 2011 01:32 PM|LINK
Hi
But i cannot seem to get it to work with my code, in your example do you mean i have to rewrite all my code.
Regards
George
raduenuca
All-Star
24675 Points
4250 Posts
Re: DropDownList
Mar 07, 2011 01:45 PM|LINK
In the action:
public ActionResult Index() { var ws = IWR.GetCurrencyDatabaseFields(); ViewData["ddlCurrency"] = new SelectList(ws, "DboCurrencyID ", "DboCountry"); return View(ws); }In the view:
<%: Html.DropDownList("ddlCurrency", ViewData["ddlCurrency"] as SelectList )%>.Radu Enuca | Blog
LearningASP_...
Member
242 Points
256 Posts
Re: DropDownList
Mar 07, 2011 01:58 PM|LINK
Thanks
Got it to work this way also:
<%: Html.DropDownList("ddlCurrency", (SelectList)(ViewData["ddlCurrency"]))%>Regards
George