में डेटाबेस से ड्रॉपडाउन सूची को कैसे पॉप्युलेट करें, शीर्षक कहता है कि मैं उस कार्य में कुछ मदद ढूंढ रहा हूं, मैंने इसके बारे में कई ट्यूटोरियल पढ़े हैं, लेकिन उनमें से कोई भी मेरी समस्या का हल नहीं कर सकता है डेटाबेस से dropdownlistfor
लोड करने के लिए। अब तक, मैं निम्नलिखित कोड है:एएसपी.नेट एमवीसी 4
**LNClientes():**
public List<ENDistrito> DistritoListar()
{
return new ADClientes().DistritoListar();
}
**ADClientes():**
public List<ENDistrito> DistritoListar()
{
Database oDatabase = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["conexionBD"]);
DbCommand odbcommand = oDatabase.GetStoredProcCommand("USP_SEL_DISTRITOS");
List<ENDistrito> lista = new List<ENDistrito>();
using (IDataReader reader = oDatabase.ExecuteReader(odbcommand))
{
while (reader.Read())
lista.Add(new ENDistrito(reader));
}
return lista;
}
**Controller:**
public ActionResult Registrar()
{
ViewBag.Message = Resources.Language.Title_Page_MC_C;
var ListaDistrito = new LNClientes().DistritoListar();
ViewBag.ObtenerDistrito = new SelectList(ListaDistrito, "IdDistrito", "DescripcionDistrito");
return View();
}
दृश्य:
<div class="editor-field">
@Html.EditorFor(model => model.DistritoCliente)
@Html.DropDownListFor(model => model.DistritoCliente,(SelectList)ViewBag.ObtenerDistrito,"--Seleccione--")
@Html.ValidationMessageFor(model => model.DistritoCliente)
</div>
, जब तक इस बिंदु सब कुछ ठीक है जब मैं उस फॉर्म को खोलने के dropdownlistfor
काम करता है, लेकिन मैं मिल गया जब मैं प्रपत्र सबमिट निम्न संदेश:
प्रकार 'IEnumerable' कुंजी 'DistritoCliente' है की कोई ViewData आइटम नहीं है।
कोई भी विचार जो मैं गलत कर रहा हूं या मैं इस समस्या को कैसे हल कर सकता हूं।
अग्रिम धन्यवाद।
एलेक्स
जब आप एक बार में अपने सभी डेटा को एक सूची में पढ़ रहे हों तो आप डेटा रीडर का उपयोग कैसे कर रहे हैं? –
क्या http://stackoverflow.com/questions/2849341/there-is-no-viewdata-item-of-type-ienumerableselectlistitem-that-has-the-key आपकी समस्या का समाधान करता है? –
मैंने लेख का पालन किया और अभी भी मेरी समस्या का समाधान नहीं किया है ~ मैंने कुछ चीजों को कंट्रोलर में बदल दिया है, मैं इसे कर रहा हूं: व्यूडाटा ["Distritos"] = नया चयनसूची (नया एलएन क्लाइंट()। DistritoListar(), "IdDistrito", "DescripcionDistrito"); लेकिन समस्या अभी भी ड्रॉपडाउन सूची की घोषणा है जो @ एचटीएमएल.ड्रॉपडाउन लिस्टफ़ोर (मॉडल => मॉडल। डिस्ट्रिटो क्लाइंट, ((सिलेक्टलिस्टस्टेम) व्यूडाटा ["डिस्ट्रिटो"])) लेकिन यह तर्कों की त्रुटि फेंकता है, इसे हल करने के लिए कोई अन्य विचार – Alex