【问题标题】:Razor Ajax form not submitting when converting string to html string将字符串转换为 html 字符串时,Razor Ajax 表单未提交
【发布时间】:2016-05-11 11:52:48
【问题描述】:

我是剃刀和显示模板的新手,所以请纠正我的错误。我正在处理一个包含 HTML 的字符串。如果我尝试converting it directly to a HtmlString,我的 Ajax 表单会拒绝提交。作为一种解决方法,我尝试使用显示模板来实现以下目标:

我有一个 ajax 表单的部分视图:

MyPartialView.cshtml

@model IEnumerable<MyProject.Areas.MyArea.Models.MyComment>

@using (Ajax.BeginForm("ActionThatHandlesMulipleSubmits", null,
new AjaxOptions
{
    HttpMethod = "POST", // HttpPost
    InsertionMode = InsertionMode.Replace, // empty the target first
    UpdateTargetId = "commentTbl" // place content within #commentTbl
}, new { @class = "form-inline" }))
{
<div class="ibox float-e-margins">
    <div class="ibox-title">...</div>
    <div class="ibox-content">
        <div class="table-responsive">
            <table id="dataTables-comments">
                <thead>...</thead>
                <tbody>
                    @{int i = 0;}
                    @foreach (var myModel in Model)
                    {
                        <tr>
                            <td>
                               <div id="summernote_@i" onclick="loadRichTextEditor(@i);">@Html.DisplayFor(modelItem => myModel.myString, "HtmlString")
                            </td>
                            <td>
                                <button type="submit" id="save_@i" class="btn   btn-primary" name="Save" value='submitAction'>Save</button>
                                <button type="submit" id="delete_@i" class="btn   btn-primary" name="Delete" value='submitAction'>Delete</button>
                            </td>
                        </tr> i++;
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
}

我的@foreach 循环包含这个

 @Html.DisplayFor(modelItem => myModel.myString, "HtmlString")

我的 DisplayTemplate 包含这个

HtmlString.cshtml

@model String

@{
    @Html.Raw(Model)
}

所以如果myString = "&lt;ul&gt;&lt;li&gt;my text&lt;/li&gt;&lt;/ul&gt;"

我想显示我的 ajax 表单

  • 我的文字

现在文本显示正确,但表单拒绝提交。如果我正常显示字符串,表单将毫无问题地提交。请帮助我做错了什么。

更新:下面是表单的渲染 HTML 源代码

【问题讨论】:

  • 是form没有提交的问题,还是post没有包含mytext字符串的问题?
  • 页面渲染完成后能否贴出表单的html源代码?
  • 尝试将&lt;button&gt; 更改为&lt;input&gt;
  • 我尝试了&lt;input type="button" id="save_@i" class="btn btn-primary" name="submitAction" value='Save'/&gt; 并得到了相同的结果。表格不提交。我检查了动作的拼写是否正确,因为没有@Html.Raw 也能正常工作。我试着检查控制台,我得到Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:59328/MyArea/MyComment/ActionThatHandlesMultipleSubmits?Length=7
  • 首先你需要做&lt;input type='submit' /&gt;。其次,如果响应状态为 500,则意味着您发送表单的操作遇到了运行时错误。这意味着您需要调试服务器端代码。您的表单实际上正在推送,否则您将无法从服务器获得响应!

标签: javascript html asp.net-mvc razor asp.net-ajax


【解决方案1】:

所以事实证明这与我的观点无关,而是与控制器有关。据我了解,由于我将 HTML 发送回服务器,这导致了问题。我加了

[ValidateInput(false)]

到控制器中的动作,它现在可以完美运行。所以我的控制器看起来像这样:

[HttpPost]
[ValidateInput(false)]
public void ActionThatHandlesMultipleSubmits(MyComment mycomment, String submitAction)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多