Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AJAX";邮政「;发布html元素时出现的问题_C#_Html_Ajax - Fatal编程技术网

C# AJAX";邮政「;发布html元素时出现的问题

C# AJAX";邮政「;发布html元素时出现的问题,c#,html,ajax,C#,Html,Ajax,我正在使用AJAX将div的内容发布到我的C#mvc控制器。 AJAX帖子如下所示: $.ajax({ url: '/Home/SaveContent', type: 'POST', data: { model: modelDataJson, activeId: activeId, contentToUpdate: $("div#" + activeId).html() }, dataType: 'json'

我正在使用AJAX将
div
的内容发布到我的C#mvc控制器。 AJAX帖子如下所示:

$.ajax({
    url: '/Home/SaveContent',
    type: 'POST',
    data: {
        model: modelDataJson,
        activeId: activeId,
        contentToUpdate: $("div#" + activeId).html()
    },
    dataType: 'json',
    success: function (data) {
        alert("Content hits controller. Id: " + activeId);
    },
    error: function () {
        alert("error");
    }
});
我使用的是一个所见即所得编辑器,它是一个id为:
activeId
的div。 所以我想发布的是这一行:
contentToUpdate:$(“div#“+activeId).html()
下面是哪个部门:

<div id="WelcomeText" data-photo="15" class="click2edit" style="display: block;">
    som content here
</div>

所以我的问题是:为什么包含HTML的帖子不起作用?我遗漏了什么,好像
contentToUpdate:$(“div#“+activeId”).html()不包含子元素?

默认情况下,MVC在绑定模型时会拒绝包含html的已发布值,因为这有潜在的危险。在你的第一篇文章中,价值是

som content here
但在第二种情况下

<p>som content here</p>
有关
allowtml
属性的更多信息,请访问

编辑

根据以下意见:

我的controller post方法看起来像这样
public void SaveContent(字符串模型、字符串activeId、字符串contentToUpdate){}

如果是这种情况,那么上述方法将不起作用,因为您没有可应用
[allowtml]
属性的模型

您有两个选择: 首先,您可以创建一个模型并向其添加属性,然后更改操作方法以采用新模型:

public class SomeModel
{
    public string Model {get; set;}
    public string ActiveId {get; set;}
    [AllowHtml]
    public string ContentToUpdate {get; set;}
}

public void SaveContent(SomeModel model)
{
    //access model.Model, model.ActiveId and model.ContentToUpdate here
}
或者,您可以保持操作方法不变,并使用
[ValidateInput(false)]
属性装饰操作方法:

[ValidateInput(false)]
public void SaveContent(string model, string activeId, string contentToUpdate)
{
    //do stuff here
}

这将阻止对发送到该操作方法的所有参数进行验证,因此您应该自己验证
model
activeId

ASP.NET有一个反XSS过滤器,它会阻止包含类似HTML的内容的请求。你不会遇到这样的事情吧?谢谢你的回答。我试图在我的属性上[AllowHtml],但随后我的saveFile按钮触发ajax停止工作。有什么问题?@user3228992-您使用的是哪个版本的MVC?另外,您是否接受控制器方法上的FormCollection?我使用的是MVC“5.1”。为什么我需要一个FormCollection?我的控制器post方法看起来像这样
public void SaveContent(string model,string activeId,string contentToUpdate){}
。Thanks@user3228992-查看我的更新。关于FormCollection的问题是一个猜测,因为如果您的方法类似于
public void SaveContent(FormCollection值){}
,则
AllowHtml
属性将不起作用。您不需要使用
FormCollection
(我也不认为您应该使用)。谢谢您的回答!我尝试了快速的
[ValidateInput(false)]
解决方案,效果非常好!我会好好看看你的另一个解决方案!
public class SomeModel
{
    public string Model {get; set;}
    public string ActiveId {get; set;}
    [AllowHtml]
    public string ContentToUpdate {get; set;}
}

public void SaveContent(SomeModel model)
{
    //access model.Model, model.ActiveId and model.ContentToUpdate here
}
[ValidateInput(false)]
public void SaveContent(string model, string activeId, string contentToUpdate)
{
    //do stuff here
}