如何绑定HtmlString?

How to bind HtmlString?

我必须显示的文本在我的文本框中显示正常,并按我想要的方式展示。但问题是当我需要更新文本时它告诉我什么都没有所以它告诉我它有一个空值。

以文本形式存在于数据库中

index.cshtml

@model MVCOrdklar2015.Models.Admin.IndholdViewModel
@using (Html.BeginForm("index", "AdminIndhold", FormMethod.Post))
        {
            <fieldset>
                <div class="form-group">
                    <div class="col-xs-12">
                        @Html.TextAreaFor(i => i.IndholdText, new
                   {
                       @class = "ckeditor"
                   })
                    </div>
                </div>
            </fieldset>
            <div class="form-group form-actions">
                <div class="col-xs-12">
                    <button type="submit" class="btn btn-effect-ripple btn-primary">Opdater</button>
                </div>
            </div>
        }

型号:

namespace MVCOrdklar2015.Models.Admin
{
    public class IndholdViewModel
    {
        public HtmlString IndholdText
        {
            get; set;
        }
    }
}

控制器:

[HttpPost]
    [ValidateInput(false)]
    public ActionResult Index(IndholdViewModel ModelInput)
    {
        if (!ModelState.IsValid)
        {
            DataLinqDB db = new DataLinqDB();
            var forsideindhold = db.forsides.FirstOrDefault(i => i.Id == 1);
            if (forsideindhold != null)
            {
                //error here
                forsideindhold.tekst = new HtmlString(ModelInput.IndholdText.ToString()).ToString();

                db.SubmitChanges();

                return RedirectToAction("index/Opdater");
            }
        }
        return View();
    }

你可以在这里看图片,看看它告诉我错误是什么。

默认模型绑定器不会绑定 HtmlString 属性 而不为其注册自定义模型绑定器。

尝试将 string[AllowHtml] 属性结合使用:

[AllowHtml]
public string IndholdText
{
    get; set;
}

在你的控制器中:

forsideindhold.tekst = ModelInput.IndholdText;

MSDN