如何保存HTML code在ASP.NET MVC2模式 [英] How to store HTML code in ASP.NET MVC2 Model

查看:160
本文介绍了如何保存HTML code在ASP.NET MVC2模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串属性这样的评论模式:

  [专栏]
公共字符串文本{搞定;组; }

注释文本可以内的所有HTML标签(我知道这是不好的,但我不得不)。但是,当我更新的对象,MVC 2转义所有HTML标记。

更新的方法是:

  [HttpPost]
公众的ActionResult编辑(INT ID = 0)
{
        注释注释= ID = = 0
            ?新评论()
            :commentRepository.Comments.First(X => x.ID == ID);        TryUpdateModel(注解);        如果(ModelState.IsValid)
        {
            commentRepository.Save(注解);
            返回RedirectToAction(查看/+ comment.ID);
        }
        其他
        {
            返回查看(注解);
        }
    }

如何能不转义更新注释文字?

P.S。我也有与列类型的问题:当我在SQL Server中前preSS从的 VARCHAR 的切换栏文本的文本的,更新模型失败:


  

数据类型的文本和nvarchar是等于运算符不兼容。


  
  

异常详细信息:System.Data.SqlClient.SqlException:数据类型的文本和nvarchar是等于运算符不兼容



解决方案

您所要做的就是关闭验证该特定输入字段。要做到这一点,你可以将此筛选器添加到你的行动:

  [ValidateInput(假)]

或者,如果你想成为特殊,只把它关掉上一个字段(也许它会在多工作,我没有测试过,虽然)做这样的事情:

  [ValidateInput(真,排除=YourFieldName)]

这将排除你的领域,但仍然检查所有其他领域。

ADDON:添加的过滤器中的Ca(AFAIK)双向进行。这里有2个样品:

  [HttpPost,ValidateInput(真,排除=YourFieldName)]
公众的ActionResult SomeAction(...){...}[HttpPost]
[ValidateInput(真,排除=YourFieldName)]
publiv的ActionResult SomeAction(...){...}

I have a comment model with string property like this:

[Column]
public string Text { get; set; }

Comment text can have all HTML tags inside (i know it's bad, but i have to). But when i update object, MVC 2 escapes all HTML tags.

Updating method is:

[HttpPost]
public ActionResult Edit(int ID=0)
{
        Comment comment= ID == 0
            ? new Comment ()
            : commentRepository.Comments.First(x => x.ID == ID);

        TryUpdateModel(comment);

        if (ModelState.IsValid)
        {
            commentRepository.Save(comment);
            return RedirectToAction("View/" + comment.ID);
        }
        else
        {
            return View(comment);
        }
    }

How can i update comment text without escaping?

P.S. Also i have problem with column type: when i switch column Text in SQL Server Express from varchar to text, updating model fails:

The data types text and nvarchar are incompatible in the equal to operator.

Exception Details: System.Data.SqlClient.SqlException: The data types text and nvarchar are incompatible in the equal to operator.

解决方案

What you have to do is turn off validation for that particular input field. To do that you can add this filter to you action:

[ValidateInput(false)]

Or if you want to be specific and only turn it off on that one field (maybe it will work on multiple, i haven't tested that though) do something like this:

[ValidateInput(true, Exclude = "YourFieldName")]

This will exclude you field but still check all other fields.

ADDON: Adding filters ca be done in (AFAIK) two way. Here are 2 samples:

[HttpPost, ValidateInput(true, Exclude = "YourFieldName")]
public ActionResult SomeAction(...) { ... }

[HttpPost]
[ValidateInput(true, Exclude = "YourFieldName")]
publiv ActionResult SomeAction(...) { ... }

这篇关于如何保存HTML code在ASP.NET MVC2模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆