2011-06-13 43 views
5

学习mvc和我试图实现一个带有3个字段的页面名称 - 姓名 - 描述 因此,在我的学习示例中,我加载员工,我应该能够创建和编辑它们。CKEditor MVC 3 Implementaion需要帮助

描述应该使用CKEditor。

  • 我可以加载员工
  • 我可以为他们节省

但是我似乎无法能,挽救了描述,例如无论在说明字段的用户类型。我在网上看到过几个例子,但没有一个能够下载解决方案,因为我似乎没有把它们放在一起。我发现这家伙一个很酷的HTML帮助,但似乎没有能够把一个例子一起 http://www.andrewbarber.com/post/CKEditor-Html-Helpers-ASPNET-MVC-Razor-Views.aspx

的问题是:

  1. 你怎么说是CKEDITOR内键入的值。
  2. 在我的viewModel中,描述一直为空
  3. ckEditor减慢了页面的创建速度。我该如何让它更快?我不需要所有的选项。
  4. 是否有一个使用mvc3的例子,我可以用作模板。

我已经做了所有的管道,如下所示:

Create.chtml

  @model MvcApplicationCKEditorIntegration.Models.EmployeeViewModel 
     @{ 
      ViewBag.Title = "Create"; 
     } 
     <h2> 
      Create</h2> 
     <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
     <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
     @using (Html.BeginForm()) 
     { 
      @Html.ValidationSummary(true) 
      <fieldset> 
       <legend>EmployeeViewModel</legend> 
       <div class="editor-label"> 
        @Html.LabelFor(model => model.FirstName) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.FirstName) 
        @Html.ValidationMessageFor(model => model.FirstName) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(model => model.LastName) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.LastName) 
        @Html.ValidationMessageFor(model => model.LastName) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(model => model.Email) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.Email) 
        @Html.ValidationMessageFor(model => model.Email) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(model => model.PhotoPath) 
       </div> 
       <div class="editor-field"> 
        @Html.EditorFor(model => model.PhotoPath) 
        @Html.ValidationMessageFor(model => model.PhotoPath) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(model => model.Description) 
       </div> 
       <div class="editor-field">    
        <textarea class="ckeditor" id="ckeditor" rows="10"></textarea>    
       </div> 
       <p> 
        <input type="submit" value="Create" /> 
       </p> 
      </fieldset> 
     } 
     <div> 
      @Html.ActionLink("Back to List", "Index") 
     </div> 
     <script type="text/javascript" src="../../ckeditor/ckeditor.js"></script> 

EmployeeController

  public class EmployeeController : Controller 
      { 
       public ActionResult Index() 
       { 
        var employeeRepository=new EmployeeRepository(); 
        var employees = employeeRepository.GetAll(); 
        var employeeList = employees.Select(employee => new EmployeeViewModel 
                     { 
                      EmployeeId = employee.EmployeeId, 
                      FirstName = employee.FirstName, 
                      LastName = employee.LastName, 
                      PhotoPath = employee.PhotoPath, 
                      Email = employee.Email, 
                      Description = employee.Description 
                     }).ToList(); 

        return View(employeeList); 
       } 

       public ActionResult Create() 
       { 

        return View(new EmployeeViewModel()); 
       } 

       [HttpPost] 
       public ActionResult Create(EmployeeViewModel vm) 
       { 
        if(ModelState.IsValid) 
        { 
         var employeeRepository=new EmployeeRepository(); 
         var emp=new Employee 
             { 
              FirstName = vm.FirstName, 
              LastName = vm.LastName, 
              Description = vm.Description, 
              Email = vm.Email, 
              PhotoPath = vm.PhotoPath 
             }; 
         employeeRepository.Insert(emp); 
         return RedirectToAction("Index"); 

        } 
        return View(vm); 
       } 




      } 
     } 

感谢您的任何建议!

EDITED例如使用CKEditor的帮手

@using MvcApplicationCKEditorIntegration.Helpers 
    @model MvcApplicationCKEditorIntegration.Models.EmployeeViewModel 
    @{ 
     ViewBag.Title = "Create"; 
    } 
    <h2> 
     Create</h2> 
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
    @Html.CKEditorHeaderScripts() 
    @using (Html.BeginForm()) 
    { 
     @Html.ValidationSummary(true) 
     <fieldset> 
      <legend>EmployeeViewModel</legend> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.FirstName) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.FirstName) 
       @Html.ValidationMessageFor(model => model.FirstName) 
      </div> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.LastName) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.LastName) 
       @Html.ValidationMessageFor(model => model.LastName) 
      </div> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.Email) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.Email) 
       @Html.ValidationMessageFor(model => model.Email) 
      </div> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.PhotoPath) 
      </div> 
      <div class="editor-field"> 
       @Html.EditorFor(model => model.PhotoPath) 
       @Html.ValidationMessageFor(model => model.PhotoPath) 
      </div> 
      <div class="editor-label"> 
       @Html.LabelFor(model => model.Description) 
      </div> 
       @Html.CKEditorFor(model=>model.Description) 
      <p> 
       <input type="submit" value="Create" onclick="@Html.CKEditorSubmitButtonUpdateFunction()" /> 
      </p> 
     </fieldset> 
    } 
    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 
    <script type="text/javascript" src="../../ckeditor/ckeditor.js"></script> 
+0

您未使用该博客文章中发布的解决方案;你根本就不在任何地方打电话给助手。 – 2011-06-13 18:31:40

回答

7

你实际上并没有使用CKEditor的助手都喜欢的博客页面上的描述(这是我自己的博客)

助手的目的是,一旦你正确地包括了代码到您的项目,你可以简单地这样做:

@Html.CKEditorFor(model=>model.Description) 

但是,你似乎简单地后“手动”创建一个纯旧文本区域,并与它的工作。没有任何东西可以将它绑定到你的财产上,就像你使用那篇文章中描述的帮手一样。

另请注意,您并未使用更新后台文本区域的代码;因此如果您的模型在Description字段中设置了Required,则在首次提交正确配置的CKEditorFor()时,您将收到客户端验证错误。这对我的帮助者来说并不是唯一的; “必需”的任何绑定属性都需要该博客文章中提到的那一点Javascript。我将它作为onclick关闭提交按钮,但您可以在任何地方运行相同的代码。你只需要将它包含在页面中,但你还没有完成。

+0

嗨,谢谢你的答复。我没有使用过你的代码,因为我不明白你是如何保存ckeditor的内容的,它肯定看起来不错。我现在要试试。你有我可以下载的解决方案吗? – user712923 2011-06-13 18:29:38

+0

你不需要做任何特殊的事情来保存内容;它和其他任何绑定属性一样工作。 – 2011-06-13 18:31:03

+0

是@HtmlCKEditorFor所有的工作,并将其保存到描述字段? – user712923 2011-06-13 18:31:21

3

你可能想尝试设置textarea的name属性为 “描述”

这样:

<div class="editor-field">    
    <textarea class="ckeditor" id="ckeditor" rows="10" name="Description"></textarea> 
</div> 

如果没有那么你可能不得不使用javascript来获取编辑器中的内容,并将其设置在帖子前的隐藏字段中。