问题
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