سؤال

أحاول إنشاء مساعد ويب حلاقة مثل هذا:

@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()
}

وماذا عن هذا؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top