【问题标题】:EditorFor isn't working on classes in different dllEditorFor 不适用于不同 dll 中的类
【发布时间】:2013-08-08 11:12:27
【问题描述】:

我有两个项目的解决方案。

其中一个包含数据访问层类(DAL.dll),另一个是 ASP.NET MVC 项目。 ASP.NET MVC 项目依赖于 DAL.dll。 我想在 ASP.NET MVC 项目的模型类中使用 DAL.dll 类。

假设我在 DAL.dll 中有一个类如下:

public class Test{

  public string Description{get;set;}

}

而且,在 ASP.NET MVC 项目中,我有一个使用 Test.cs 的模型类,如下所示:

public class ModelClass{

  Test CrudModel{get;set;}

  public string Description{get;set;} 

}

而且,在我看来 (*.cshtml) 我有以下代码:

@Html.EditorFor(model => model.CrudModel.Description, "tinymce_jquery_min")

在上面的代码中,@Html.EditorFor 没有渲染“tinymce_jquery_min”模板。

但是,当我使用以下代码时,@Html.EditorFor 会渲染“tinymce_jquery_min”模板:

 @Html.EditorFor(model => model.Description, "tinymce_jquery_min")

为什么@Html.EditorFor 只为 ASP.NET MVC 项目中模型类中的属性呈现模板,而不为不同 dll 中的属性呈现模板?

更新

“tinymce_jquery_min”模板如下:

<script src="@Url.Content("~/Scripts/tinymce/jquery.tinymce.js")"      type="text/javascript"></script>

   <script type="text/javascript">
   $(document).ready(function () {
        setTimeout(loadTinyMCE, 500);
    });

  function loadTinyMCE() {
      $('#@ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)').tinymce({


              script_url: '@Url.Content("~/Scripts/tinymce/tiny_mce.js")',
              theme: "advanced",

              height: "300",
              width: "400",
              verify_html: false,

              theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
                      theme_advanced_toolbar_location: "top",
              theme_advanced_toolbar_align: "left",
              theme_advanced_statusbar_location: "bottom",
              theme_advanced_resizing: false,


              convert_urls: false,


              template_external_list_url: "lists/template_list.js",
              external_link_list_url: "lists/link_list.js",
              external_image_list_url: "lists/image_list.js",
              media_external_list_url: "lists/media_list.js"

          });
  }





 </script>
        @Html.TextArea(string.Empty,  
          ViewData.TemplateInfo.FormattedModelValue  
       )

【问题讨论】:

  • 你的“tinymce_jquery_min”局部视图在哪里?
  • ataravati,我用“tinymce_jquery_min”模板更新了帖子。
  • 不,我不想看到代码。模板在哪里,在哪个文件夹中?
  • 但是,无论如何,为什么您的局部视图不是强类型的?
  • 对不起,我不明白你的问题。我的模板位于“Views/Shared/EditorTemplates”。你能指导我如何对我的模板进行强类型化吗?我是新手ASP MVC。

标签: asp.net-mvc tinymce editorfor


【解决方案1】:

这就是你如何使你的局部视图成为强类型的。定义模型(在本例中为字符串类型),并使用Html.TextAreaFor 呈现您的 TextArea:

@model string

<script src="@Url.Content("~/Scripts/tinymce/jquery.tinymce.js")" type="text/javascript"></script>

<script type="text/javascript">
   $(document).ready(function () {
      setTimeout(loadTinyMCE, 500);
   });

   function loadTinyMCE() {
      $('#@ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)').tinymce({


              script_url: '@Url.Content("~/Scripts/tinymce/tiny_mce.js")',
              theme: "advanced",

              height: "300",
              width: "400",
              verify_html: false,

              theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
                      theme_advanced_toolbar_location: "top",
              theme_advanced_toolbar_align: "left",
              theme_advanced_statusbar_location: "bottom",
              theme_advanced_resizing: false,


              convert_urls: false,


              template_external_list_url: "lists/template_list.js",
              external_link_list_url: "lists/link_list.js",
              external_image_list_url: "lists/image_list.js",
              media_external_list_url: "lists/media_list.js"

      });
   }
</script>

@Html.TextAreaFor(model => model)

【讨论】:

    【解决方案2】:

    感谢ataravati 的帮助。 我的模板中似乎有一个错误。 我必须改变

       ViewData.TemplateInfo.GetFullHtmlFieldName
    

       ViewData.TemplateInfo.GetFullHtmlFieldId
    

    因为 ASP MVC 在以下代码中为 id 属性和 name 属性创建了不同的值:

    @Html.EditorFor(model => model.CrudModel.Description, "tinymce_jquery_min")
    

    然后

      ViewData.TemplateInfo.GetFullHtmlFieldName
    

    不工作。

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多