asp.net – CKEditor MVC 3实现
学习mvc,我正在尝试实现一个包含3个字段Name-Surname-Description的页面
所以在我的学习示例中,我正在加载员工,我应该能够创建和编辑它们. 描述应该使用CKEditor. >我可以加载员工 但是我似乎无法保存描述,例如用户在描述字段中输入的内容.我在网上看到的例子很少,但没有一个下载的解决方案,因为我似乎无法整理.我发现这个人有一个很酷的html助手,但似乎无法将一个例子放在一起 问题是: >如何获得在ckEditor中输入的值. 我做了所有的管道如下: 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); } } } 谢谢你的任何建议! 编辑示例使用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> 解决方法
您实际上并没有像在该博客页面(这是我自己的博客)中描述的那样使用CKEditor助手.
该帮助程序的目的是,一旦您将代码正确地包含在项目中,您就可以这样做: @Html.CKEditorFor(model=>model.Description) 但是,您似乎只是创建一个普通的文本区域并在此之后“手动”使用它.没有任何东西可以将它绑定到您的属性,如果您使用了该帖子中描述的帮助程序就会存在. 另请注意,您没有使用更新幕后文本区域的代码;因此,如果您的模型在“描述”字段中设置了“必需”,则在第一次提交其他正确配置的CKEditorFor()时,您将收到客户端验证错误.这对我的助手来说并不是唯一的;任何“必需”的绑定属性也需要该博客文章中提到的Javascript位.我将其作为onclick关闭提交按钮,但您可以在任何地方运行相同的代码.您只需要将其包含在您尚未完成的页面中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 带有Expression Web 4的ASP.NET MVC(RAZOR)
- asp.net – 使用StartMode =“AlwaysRunning”在ASP .NET 4
- asp.net-mvc-3 – 来自控制器的ASP.NET MVC3 View的物理位置
- asp.net-mvc – Sitecore:添加到此处按钮未显示在占位符上
- [你必须知道的.NET] 第五回:深入浅出关键字---把new说透
- asp.net – 当passwordFormat = Encrypted和decryption = A
- asp.net – 我可以在超链接上显式指定NavigateUrl吗?
- Asp.Net Core 获取找回密码的Code提示错误No IUserTokenPro
- asp.net-mvc-3 – 如何使用ViewBag属性从我的View中设置Jav
- ASP.NET 5 / MVC 6数据库优先 – 在应用程序配置文件中找不
- asp.net-mvc-2 – EditorFor – 传入字典的模型项
- asp.net-mvc – CssRewriteUrlTransform没有被调
- Asp.Net – 什么是<%$?
- asp.net-mvc – 直接在MVC中指定视图位置更有效吗
- ASP.NET自定义控件:何时调用LoadPostData()?
- ASP.NET Identity 2是否使用machinekey来密码密码
- ASP.NET(.asmx)webservices中的客户端IP地址
- asp.net-mvc-4 – Web API 2返回OK响应,但在后台
- 来自UserControl的ASP.NET AJAX页面方法
- asp.net-mvc – 疑难解答反伪造令牌问题