将羽毛笔绑定到ASP网络核心模型

时间:2019-06-18 18:43:42

标签: asp.net-mvc asp.net-core razor quill

我正在使用ASP.Net Core表单通过代码将数据从用户传递到服务器:

@model SomeModel
@using(Html.BeginForm(...))
{
    @Html.TextBoxFor(s => s.PropertyName);
}

它可以正常工作,但现在我有需要从主轴编辑器填充的字段。羽毛笔要求我将他的div容器传递给他,以完成他的工作,但我不知道如何将该容器绑定到我的模型属性。我尝试给该div idname作为属性名称,但是它没有用。还尝试添加runat = server,但仍然无法正常工作。

这是我的完整代码:

@model ClanakModel
@{
    ViewData["Title"] = "New story";
}

<style>
    #Naslov {
        width: 80%;
        margin-bottom: 20px;
    }

    #GrupaID {
        height: 30px;
        width: 18%;
        margin-left: 2%;
    }

    input {
        border: 1px solid gray;
        background-color: whitesmoke;
        padding: 5px;
        border-radius: 20px 20px 20px 20px;
    }

    #Tekst {
        width: 100%;
        max-width: 100%;
        min-width: 100%;
    }

    #Publish_btn {
        float: right;
        border: 2px solid black;
        background-color: white;
        margin-top: 20px;
        border-radius: 20px;
        padding: 5px 15px 5px 15px;
    }

    #Publish_btn:hover {
        background-color: deepskyblue;
    }
</style>

<h2>Write new story</h2>

@using (Html.BeginForm("CreateNew", "Story", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(c => c.Naslov, new { @placeholder = "Title" });
    @Html.DropDownListFor(c => c.GrupaID, new SelectList(GrupaModel.List(), "GrupaID", "Naziv"));
    <div id="Tekst" name="Tekst"></div>

    <button id="Publish_btn">Publish</button>
}
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
<script>
    var toolbarOptions = [
        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
        ['image'],
        ['blockquote', 'code-block'],

        [{ 'header': 1 }, { 'header': 2 }],               // custom button values
        [{ 'list': 'ordered' }, { 'list': 'bullet' }],
        [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
        [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
        [{ 'direction': 'rtl' }],                         // text direction

        [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
        [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

        [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
        [{ 'font': [] }],
        [{ 'align': [] }],

        ['clean']                                         // remove formatting button
    ];
    var options = {
        theme: 'snow',
        placeholder: 'Start writing here!',
        modules: {
            toolbar: toolbarOptions
        }
    };
    var quill = new Quill('#Tekst', options);
</script>

1 个答案:

答案 0 :(得分:0)

由于DIV不是输入元素,因此您可以添加hidden输入,以便在提交表单时接受编辑器的内容。您可以使用quill.getContents()在js中获取编辑器内容。

假设您的模型有一个Description字段,下面是一个简单的演示:

@model SomeModel
<h2>Write new story</h2>

<form id="form" method="post">

    <input asp-for="Description" name="Description" type="hidden" class="form-control" />
    <div id="Tekst" name="Tekst"></div>

    <input type="submit" id="Publish_btn" value="Publish" />
</form>
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
<script>
var toolbarOptions = [
    ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
    ['image'],
    ['blockquote', 'code-block'],

    [{ 'header': 1 }, { 'header': 2 }],               // custom button values
    [{ 'list': 'ordered' }, { 'list': 'bullet' }],
    [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
    [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
    [{ 'direction': 'rtl' }],                         // text direction

    [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
    [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

    [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
    [{ 'font': [] }],
    [{ 'align': [] }],

    ['clean']                                         // remove formatting button
];
var options = {
    theme: 'snow',
    placeholder: 'Start writing here!',
    modules: {
        toolbar: toolbarOptions
    }
};
var quill = new Quill('#Tekst', options);

var form = document.querySelector('form');
form.onsubmit = function () {
    // Populate hidden form on submit
    var description = document.querySelector('input[name=Description]');
    description.value = quill.getContents();
};
</script>