简体   繁体   English

将CKEDITOR值发布到Asp.net Core中

[英]Post CKEDITOR value to action in Asp.net Core

Im using CKEditor 4 and Im going to post ckeditor html value to Action. 我正在使用CKEditor 4,我将ckeditor html值发布到Action。 but when i click on submit buttom and model post to action the ckeditor value posted null value. 但是,当我单击提交按钮和模型发布操作时,ckeditor值发布为空值。

View : 查看:

<form asp-controller="Book" asp-action="AddEditBook" id="addeditbook" data-ajax="true" 
  data-ajax-method="POST" data-ajax-update="#addeditbook" data-ajax-mode="replace">


<div class="modal-body form-horizontal">
    <div class="row">

        <div class="form-group">
            <label asp-for="BookName" class="col-lg-2 col-sm-2 control-label"></label>
            <div class="col-lg-6">
                <input asp-for="BookName" class="form-control" />
                <span asp-validation-for="BookName" class="text-danger"></span>
            </div>
        </div>

        <div class="form-group">
            <label asp-for="BookDescription" class="col-lg-2 col-sm-2 control-label"></label>
            <div class="col-lg-9">
                <textarea id="editor1" name="editor1" asp-for="BookDescription" class="form-control"></textarea>
                <span asp-validation-for="BookDescription" class="text-danger"></span>
            </div>

            <script type="text/javascript">
                CKEDITOR.replace('editor1');
            </script>
        </div>
    </div>
</div>


<input type="submit" value="submit"/>

}

Model: 模型:

 public class AddEditBookViewModel
{
    [Key]
    public int BookId { get; set; }

    [Display(Name = "Book Name:")]
    [Required(ErrorMessage = "Enter Book Name")]
    public string BookName { get; set; }

    [Display(Name = "Book Description:")]
    [Required(ErrorMessage = "Enter Book Description")]
    public string BookDescription { get; set; }
}

Controller : 控制器:

   [HttpPost]
    public IActionResult AddEditBook(AddEditBookViewModel model)
    {
        if (ModelState.IsValid)
        {
                using (var db = _iServiceProvider.GetRequiredService<ApplicationDbContext>())
                {
                    Book bookModel = Mapper.Map<AddEditBookViewModel, Book>(model);
                    db.books.Add(bookModel);
                    db.SaveChanges();
                }
                return view("index");
        }
}

How can i post ckeditor html value to action to save or edit? 如何发布ckeditor html值以保存或编辑?

Forms post back the name/value pairs of its successful form controls and the ModelBinder binds your model based on those values. 窗体回传成功窗体控件的名称/值对,然后ModelBinder根据这些值绑定模型。 Your model contains a property named BookDescription but you have overwritten the name attribute generated by the TagHelper (which is name="BookDescription" ) and given it name="editor1" which is not the name of a property in your model. 您的模型包含一个名为BookDescription的属性,但是您已经覆盖了TagHelper生成的name属性(即name="BookDescription" ),并name="BookDescription"指定了name="editor1" ,这不是模型中属性的名称。

Remove the name="editor1" attribute so that you input is 删除name="editor1"属性,以便您输入

<textarea id="editor1" asp-for="BookDescription" class="form-control"></textarea>

As a side note, the TagHelper also generated id="BookDescription" by default, and there is no need to overwrite that attribute either. 另外,默认情况下,TagHelper还会生成id="BookDescription" ,并且也无需覆盖该属性。 You could also delete the id="editor1" attribute and the script would then be 您也可以删除id="editor1"属性,然后脚本将是

CKEDITOR.replace('BookDescription');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM