gpt4 book ai didi

asp.net-mvc - Html.BeginForm() 工作正常, Html.BeginForm ("action","controller")忽略 [AllowHtmlAttribute]

转载 作者:行者123 更新时间:2023-12-01 23:51:32 29 4
gpt4 key购买 nike

我正在使用TinyMCE editor在我网站的管理面板上,所以我用 [AllowHtml] 装饰模型属性(tinymce 的目标),并在 View 中使用 Html.BeginForm() 。当我提交带有 HTML 字段的表单时,一切正常。

但是如果我以同样的方式使用重载 Html.BeginForm("action","controller") ,我就会遇到问题,它会跳过 [AllowHtml]并抛出众所周知的 Request.form 异常。我被迫在 Action-Method 上使用 [ValidateInput(false)] 来使其正常工作。你知道为什么吗?预先感谢您的澄清,

这是场景/项目:Asp.net Mvc 4:

模型/Ricetta.cs

..
[Required(ErrorMessage = "Corpo Articolo vuoto")]
[AllowHtml]
public string corpoTesto { get; set; }
..

Controller /RicetteController.cs

..
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(RicettaViewModel modelloRicetta)
{
if (ModelState.IsValid) {
..

查看 Ricette/Create 从 RicetteController 中的另一个操作方法调用为 View("Create", modelObject)

 @model WebAPP_MVC4.Areas.Admin.Models.RicettaViewModel
...
@using (Html.BeginForm("Create","Ricette",FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

....

<fieldset>
<legend>Corpo Ricetta ~</legend>
<div class="editor-label">
@Html.LabelFor(p=>p.ricetta.corpoTesto)
</div>
<div class="editor-field">
@Html.TextAreaFor(p=>p.ricetta.corpoTesto, new { @cols = 60, @rows = 20})
@Html.ValidationMessageFor(p=>p.ricetta.corpoTesto)
</div>
</fieldset>
..

最佳答案

我进行了快速测试,一切正常,Html.BeginForm() 和 Html.BeginForm("action","controller") 之间的行为没有差异。也许这个问题的原因在于您没有向我们展示的源代码中。

下面是我的代码(有效):
View 模型:

public class PostViewModel
{
[AllowHtml]
[Required]
public string Content { get; set; }
}

Controller :

public ActionResult Index()
{
return View("Create", new PostViewModel());
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PostViewModel model)
{
if (ModelState.IsValid)
{
return Index();
}
return View(model);
}

查看:

@model SendHTmlTpControler.Models.PostViewModel

<html>
<head>
<script src="~/Scripts/tinymce/tiny_mce.js"></script>

<script type="text/javascript">
tinymce.init({
selector: "textarea",
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
</head>
<body>
<h2>Create</h2>

@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<div class="editor-label">
@Html.LabelFor(model => model.Content)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Content, new { @cols = 60, @rows = 20 })
@Html.ValidationMessageFor(model => model.Content)
</div>

<p>
<input type="submit" value="Save" />
</p>
}

</body>
</html>

关于asp.net-mvc - Html.BeginForm() 工作正常, Html.BeginForm ("action","controller")忽略 [AllowHtmlAttribute],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995835/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com