我正在尝试将内容从富文本编辑器(在我的情况下为ckeditor)保存到我的数据库中的blob字段。
这是我的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; }
}
在我看来,文章正文是我的富文本字段:
<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>
在我的控制器中的行动中:
[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);
}
但在我的存储库中,我得到:Cannot convert type 'string' to 'byte[]'
存储库:
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
}
有人可以给我一个开始或帮助我吗?
答案 0 :(得分:1)
应该是存储库方法格式,如
Public void AddArticle(string title, string Description, string ArticleBody)
{
//logic
}
我认为您的存储库方法对任何一个参数都有字节类型。检查一下,就像我的方法格式一样。
修改强>
检查数据库中的article_body
列数据类型?我应该Nvarchar(Max)
。