देखें मैंने अपने एमवीसी 3 (एएसपीएक्स) में निम्नलिखित का उपयोग किया। .NETFramework 4.0 बहुत अच्छा काम करता है।SelectListItem/अद्यतन फॉर्म एमवीसी 3 4.0 को एमवीसी 5 4.5.1/एक्सटेंशन विधि
दृश्य पेज विस्तार विधि:
public static List<SelectListItem> GetDropDownListItems<T>(this ViewPage<T> viewPage, string listName, int? currentValue, bool addBlank)
where T : class
{
List<SelectListItem> list = new List<SelectListItem>();
IEnumerable<KeyValuePair<int, string>> pairs = viewPage.ViewData[listName] as IEnumerable<KeyValuePair<int, string>>;
if (addBlank)
{
SelectListItem emptyItem = new SelectListItem();
list.Add(emptyItem);
}
foreach (KeyValuePair<int, string> pair in pairs)
{
SelectListItem item = new SelectListItem();
item.Text = pair.Value;
item.Value = pair.Key.ToString();
item.Selected = pair.Key == currentValue;
list.Add(item);
}
return list;
}
आंशिक मॉडल:
public static Dictionary<int, string> DoYouSmokeNowValues = new Dictionary<int, string>()
{
{ 1, "Yes" },
{ 2, "No" },
{ 3, "Never" }
};
public static int MapDoYouSmokeNowValue (string value)
{
return (from v in DoYouSmokeNowValues
where v.Value == value
select v.Key).FirstOrDefault();
}
public static string MapDoYouSmokeNowValue (int? value)
{
return (from v in DoYouSmokeNowValues
where v.Key == value
select v.Value).FirstOrDefault();
}
public string DoYouSmokeNow
{
get
{
return MapDoYouSmokeNowValue(DoYouSmokeNowID);
}
set
{
DoYouSmokeNowID = MapDoYouSmokeNowValue(value);
}
}
ध्यान में रखते हुए:
@Html.ExDropDownList("DoYouSmokeNowID", this.GetDropDownListItems("DoYouSmokeNowValues", this.Model.PersonalSocial.DoYouSmokeNowID, false), this.isReadOnly)
जब मैं MVC5 .NETFramework 4.5.1 के लिए आवेदन अपडेट किया गया। सबसे पहले मैं GetDropDownListItems को हल नहीं कर सका, इसलिए मैंने इसे @functions का उपयोग करके एक्सटेंशन मॉडल से दृश्य में कॉपी किया, मुझे यह त्रुटि मिल गई।
The type argument for method 'IEnumerable<SelectedItem> ASP._Page_Views_Visit_PhysicalExam_cshtml.GetDropDownListItems<T>(ViewPage<T>, string,,int?,bool)' cannot be inferred from the usage. Try specifying the the type arguments explicity.
एक दूसरी बात, MVC3 समाधान, एक परियोजना था, जबकि MVC5 बहु परत है और मैं डोमेन परत में मॉडल है, जबकि दृश्य विस्तार विचारों के रूप में परियोजना है।
मेरा सवाल है कि मैं पृष्ठ दृश्य एक्सटेंशन विधि को हल नहीं कर सकता हूं?
आपके सुझावों की सराहना करेंगे।
'GetDropDownListItems' विधि 2 पैरामीटर' bool ?, bool उम्मीद 'लेकिन आपके पास तीन' स्ट्रिंग, बूल ?, बूल 'गुजर रहा है। –
आप सही स्टीफन हैं, मैंने गलत विधि पोस्ट की है। मैंने व्यू एक्सटेंशन विधि के बारे में अधिक जानकारी के साथ प्रश्न अपडेट किया। – hncl
क्या त्रुटि संदेश अभी भी वही है? (इसका कुछ अर्थ नहीं निकलता)। मेरा सवाल यह है कि यह सब कुछ वैसे भी किया जा रहा है, और '@ Html.ExDropDownList() 'क्या है? ऐसा लगता है कि यह एमवीसी के अंतर्निहित विधियों –