2017-10-20 68 views
0

我在我的视图中有一个字段,名为Comments,每次我在'输入'中包含<之前的任何其他字符(字母等)时,动作控制器不会所谓的,我想是因为不能够正确地解析字符串,发送输入字符串字段值给动作控制器

这是怎么在我的课的属性声明:

[Display(Name = "Comments"), DataType(DataType.MultilineText)] 
public string my_comments { get; set; } 

它当我输入任何文字能正常工作,例如:

dog[email protected]>asa?

但是,如果我尝试类似:

<[email protected]><p<asa>asasd<f

的动作没有被调用,我想是因为这是不能够解析输入到一个字符串...

如果我在末尾包含<字符,它没有问题通过..例如:

ddd<

我在我看来,使用JQuery:

$("#btnSubmit").click(function() { 
$.ajax({ 
       type: 'post', 
       url: $("#btnSubmit").data("url"), 
       data: $("#formEd").serialize(),    
       success: function (result) { 
.... 
}}) 
}) 

HTML:

<div class="form-group"> 
     <div class="control-label col-md-2">Comments</div> 
     <div class="col-md-5"> 
      @Html.EditorFor(model => model.my_comments , new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.my_comments , "", new { @class = "text-danger" }) 
     </div> 
    </div> 

我的动作控制器:

[HTTPPost] 
public ActionResult MyAction(MyClass parameter) // where MyClass contains the my_comments property... 

回答

1

您可以添加属性禁用输入验证,但你必须做出确定你确定你想允许html。

[ValidateInput(false)] 
[HTTPPost] 
public ActionResult MyAction(MyClass parameter) 

你也可以添加属性到你的变量允许有html。

[Display(Name = "Comments"), DataType(DataType.MultilineText)] 
[AllowHtml] 
public string my_comments { get; set; } 
+0

它的工作,我会选择你的答案,一旦需要的以分钟来选择它去,有一个问题:为什么输入验证不允许'<'之前字符在这种情况下,任何其他字符? – AlexGH

+1

,因为它认为它可能是html或脚本,并且出于安全原因不允许它在后发送。 –

相关问题