باستخدام مساعدي HTML في Razor Web Helper
-
26-09-2019 - |
سؤال
أحاول إنشاء مساعد ويب حلاقة مثل هذا:
@helper DisplayForm() {
@Html.EditorForModel();
}
لكن هذا يعطي الخطأ "CS0103: The name 'Html' does not exist in the current context"
.
هل هناك أي طريقة للرجوع إلى مساعدي HTML ضمن مساعدي الويب؟
المحلول
يمكنك إلقاء خاصية الصفحة الثابتة من السياق إلى النوع الصحيح:
@helper MyHelper() {
var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
Html.RenderPartial("WhatEver");
@Html.EditorForModel();
}
نصائح أخرى
المساعدون التعريفيون في الحلاقة هم طرق ثابتة. يمكنك تمرير المساعد HTML كحجة:
@helper DisplayForm(HtmlHelper html) {
@html.EditorForModel();
}
@DisplayForm(Html)
Razor Inline Webhelper هو إنشاء طريقة ثابتة.
لذلك لا يمكن الوصول إلى عضو مثيل.
@helper DisplayForm(HtmlHelper html){
@html.DisplayForModel()
}
وماذا عن هذا؟
لا تنتمي إلى StackOverflow