简体   繁体   English

Blazor 文本编辑器无法在表单上绑定值(创建/编辑)

[英]Blazor Text Editor not able to bind value on form (create/edit)

I am using Blazor Text Editor from source below.我正在使用来自以下源的 Blazor 文本编辑器。

Source - https://github.com/Blazored/TextEditor来源 - https://github.com/Blazored/TextEditor

I successfully integrated it with my create and edit form, however not able to bind-Value to it.我成功地将它与我的创建和编辑表单集成,但无法将值绑定到它。 Because of this my Data Annotation Validation is failing.因此,我的数据注释验证失败。

Internally blazor is using Quill Editor, I am not looking for javascript option.内部 blazor 正在使用 Quill 编辑器,我不是在寻找 javascript 选项。

Sample Code of editor编辑器示例代码

<BlazoredTextEditor  @ref="@QuillNative" Placeholder="Enter non HTML format like centering...">
      <ToolbarContent>Some editor stuff here</ToolbarContent>
<BlazoredTextEditor

Could anyone please help me.谁能帮帮我。 How to bind-Value or correct approach without javascript.如何在没有 javascript 的情况下绑定值或更正方法。

Vencovsky - thanks of you prompt response, I was already aware of these methods however was curious to know if anybody had tried different option. Vencovsky - 感谢您的及时回复,我已经知道这些方法,但是很想知道是否有人尝试过不同的选择。

Below is what I did..下面是我做的..

FORM -- This is common form for create & edit. FORM - 这是创建和编辑的常用表单。 OnValidSubmit will call respective Create/Edit method. OnValidSubmit 将调用相应的 Create/Edit 方法。

<EditForm Model="Entity" class="contact-form" OnValidSubmit="OnValidSubmit">
//My form fields here
//Commented the validation from that particular field
@*<ValidationMessage For="@(() =>Entity.field)" />*@

<div class="col-sm-1">
    <button type="submit" @onclick="***getEditorData***" class="btn" 
    style="border:2px solid #555555;"><span>Save</span></button>
 </div>
</EditForm>

METHOD -- getEditorData() gets fired before OnValidSubmit()方法 -- getEditorData() 在 OnValidSubmit() 之前被触发

public async void getEditorData()
{
  Enity.field = await this.QuillNative.GetHTML();
}

So in my final Entity on OnValidSubmit() I receive all fields along with editor data..因此,在 OnValidSubmit() 上的最终实体中,我收到所有字段以及编辑器数据。

Hope this help if anyone is trying to do so..如果有人试图这样做,希望这会有所帮助..

Apparently you can't bind a value to it, but you should use the provided methods显然你不能绑定一个值,但你应该使用提供的方法

Methods方法

  • GetText - Gets the content of the editor as Text. GetText - 将编辑器的内容作为文本获取。
  • GetHTML - Gets the content of the editor as HTML. GetHTML - 获取编辑器的内容为 HTML。
  • GetContent - Gets the content of the editor in the native Quill JSON Delta format. GetContent - 以原生 Quill JSON Delta 格式获取编辑器的内容。
  • LoadContent (json) - Allows the content of the editor to be programmatically set. LoadContent (json) - 允许以编程方式设置编辑器的内容。
  • LoadHTMLContent (string) - Allows the content of the editor to be programmatically set. LoadHTMLContent (string) - 允许以编程方式设置编辑器的内容。
  • InsertImage (string) - Inserts an image at the current point in the editor. InsertImage (string) - 在编辑器的当前点插入图像。

To use these methods you need a reference of it要使用这些方法,您需要它的参考

@* Getting the BlazoredTextEditor reference*@
<BlazoredTextEditor @ref="@BlazoredTextEditorRef">
    @* rest of the code*@
</BlazoredTextEditor>

And in some code in your class you can do在你的 class 的一些代码中你可以做

void LoadData(){
    //var html = BlazoredTextEditor.LoadHTML(SomeDataToLoad)
    BlazoredTextEditor.LoadText(SomeDataToLoad)
}

void ValidateData(){
    //var html = BlazoredTextEditor.GetHTML()
    var text = BlazoredTextEditor.GetText()
    // do something to validate text
}

You can call these methods and use the referece in other methods, this is just an example on how to do it.您可以调用这些方法并在其他方法中使用引用,这只是一个示例。

here is how I did this: 1- to bind the value on load:我是这样做的: 1- 绑定加载值:

 <BlazoredTextEditor @ref="@QuillHtml">
        <EditorContent>
            @((MarkupString)infoBlock.Description)
        </EditorContent>
    </BlazoredTextEditor>
  1. to get value on submit在提交时获得价值

     <EditForm Model="infoBlock" OnValidSubmit="LocalOnValidSubmit">

    ... ...

     @code {.... [Parameter] public EventCallback OnValidSubmit { get; set; } BlazoredTextEditor QuillHtml = new BlazoredTextEditor(); private async Task LocalOnValidSubmit() { infoBlock.Description = await this.QuillHtml.GetHTML(); await OnValidSubmit.InvokeAsync(this);//to call event handler passed by parent after the HTML prepared for main bound class } }

暂无
暂无

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

相关问题 从类中删除必需属性但MVC3仍然不会在文本框中没有值的情况下发布表单 - Removing Required Attribute from Class but MVC3 still won't post the form without a value in the text box 在提交/验证时,不会在错误消息中使用 Blazor 表单中 InputNumber 控件上的 DisplayName 设置 - Setting DisplayName on InputNumber control in Blazor form is NOT used in ErrorMessage upon submit/validation 如何将“必需”属性添加到 mvc razor viewmodel 文本输入编辑器 - How to add "required" attribute to mvc razor viewmodel text input editor MVC模型的唯一字段创建-编辑并发症 - MVC models' unique field Create-Edit Complication 在Silverlight中验证创建用户表单 - Validation of create user form in Silverlight 是否可以为生日创建自定义编辑器,而不必覆盖日期的自定义编辑器? - Is it possible to create a Custom Editor for Birthdate that doesn't necessarily override the one for Date? blazor 页面中列表的数据注释 - Dataannotations for list in blazor page 使用 Blazor 处理数据注释 - Working with Data Annotations with Blazor ASP.NET MVC 2-在编辑操作中处理文件; 或者,是否可以创建一个“可选”数据注释以跳过其他属性? - ASP.NET MVC 2 - Handling files in an edit action; Or, is it possible to create an 'Optional' data annotation which would skip over other attributes? Blazor WebAssembly 客户端数据注释本地化 - Blazor WebAssembly Client DataAnnotations Localization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM