Save content rich text editor to blob field in mysql database

依然范特西╮ 提交于 2019-12-12 00:45:08

问题


I'm trying to save the content from a rich text editor (ckeditor in my case) to my a blob field in my database.

This is my ViewModel:

public class ArticleViewModel
{
    [Required]
    [Display(Name = "Title")]
    public string Title { get; set; }

    [Required]
    [Display(Name = "Description")]
    public string Description { get; set; }

    [Required]
    [Display(Name = "Article Body")]
    public string ArticleBody { get; set; }

}

The Article Body is my rich text field like this in my view:

<div class="editor-label">
    @Html.LabelFor(model => model.ArticleBody)
</div>
<div class="editor-field">
    @Html.TextAreaFor(model => model.ArticleBody, new { placeholder = "Type the content of the article", @class = "ckeditor" })
    @Html.ValidationMessageFor(model => model.ArticleBody, string.Empty)
</div>

In my Action in my Controller:

[HttpPost]
    public ActionResult Create(ArticleViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // Get the userID who created the article
                User usr = userrepo.FindByUsername(User.Identity.Name);
                model.UsernameID = usr.user_id;

                repository.AddArticle(model.Title, model.Description, model.ArticleBody);
            }
            catch (ArgumentException ae)
            {
                ModelState.AddModelError("", ae.Message);
            }

            return RedirectToAction("Index");

        }

        return View(model);
    }

But in my repository I get : Cannot convert type 'string' to 'byte[]'

Repository:

public void AddArticle(string Title, string Description, string ArticleBody)
    {
        item Item = new item()
        {
            item_title = Title,
            item_description = Description,
            article_body = ArticleBody,
            item_createddate = DateTime.Now,
            item_approved = false,
            user_id = 1,
            district_id = 2,
            link = "",
            type = GetType("Article")
        };

        try
        {
            AddItem(Item);
        }

        catch (ArgumentException ae)
        {
            throw ae;
        }
        catch (Exception)
        {
            throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
                "If the problem persists, please contact your system administrator.");
        }

        Save();
        // Immediately persist the User data

    }

Can somebody give me a start or help me with this?


回答1:


Should be Repository method format like

Public void AddArticle(string title, string  Description, string ArticleBody)
{
//logic
}

I think your repository method have byte type for any one argument . Check that like my method format .

Edit:

Check your article_body column data type in your database? its should me Nvarchar(Max) .



来源:https://stackoverflow.com/questions/18362140/save-content-rich-text-editor-to-blob-field-in-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!