मेरा मानना है कि मैंने अपना डेटा सही तरीके से बाध्य कर दिया है, लेकिन मुझे ठीक से दिखाने के लिए प्रत्येक selectListItem के लिए मेरी टेक्स्ट प्रॉपर्टी नहीं मिल रही है।मुझे अपने DropDownList में "System.Web.Mvc.SelectListItem" क्यों मिल रहा है?
मेरे मॉडल:
public class Licenses
{
public SelectList LicenseNames { get; set; }
public string SelectedLicenseName { get; set; }
}
नियंत्रक:
[HttpGet]
public ActionResult License()
{
try
{
DataTable LicsTable = BW.SQLServer.Table("GetLicenses", ConfigurationManager.ConnectionStrings["ProfressionalActivitiesConnection"].ToString());
ProfessionalActivities.Models.Licenses model = new ProfessionalActivities.Models.Licenses();
model.LicenseNames = new SelectList(LicsTable.AsEnumerable().Select(row =>
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}));
return PartialView("_AddLicense", model);
}
catch (Exception ex)
{
var t = ex;
return PartialView("_AddLicense");
}
}
दृश्य:
@Html.DropDownList("LicenseNames", new SelectList(Model.LicenseNames, "Value", "Text", Model.LicenseNames.SelectedValue), new { htmlAttributes = new { @class = "form-control focusMe" } })
श्याजू के उत्तर के अलावा, ध्यान दें कि 'DropDownList() 'का दूसरा पैरामीटर' IEnumerable 'है। आप 'नई selectListItem {Value = row ["विवरण"] का उपयोग करके बनाते हैं। ToString(), टेक्स्ट = "test"})' जो आपको चाहिए वह सब कुछ है। 'नई सिलेक्टलिस्ट() '(जहां आप' टेक्स्ट' और 'वैल्यू' गुण सेट करना भूल गए हैं) का उपयोग कर नियंत्रक में दूसरा समान बनाना और फिर दृश्य में तीसरा एक समान बनाना केवल व्यर्थ अतिरिक्त ओवरहेड है। –