【发布时间】: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 = "<ul><li>my text</li></ul>"
我想显示我的 ajax 表单
- 我的文字
现在文本显示正确,但表单拒绝提交。如果我正常显示字符串,表单将毫无问题地提交。请帮助我做错了什么。
更新:下面是表单的渲染 HTML 源代码
【问题讨论】:
-
是form没有提交的问题,还是post没有包含mytext字符串的问题?
-
页面渲染完成后能否贴出表单的html源代码?
-
尝试将
<button>更改为<input> -
我尝试了
<input type="button" id="save_@i" class="btn btn-primary" name="submitAction" value='Save'/>并得到了相同的结果。表格不提交。我检查了动作的拼写是否正确,因为没有@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 -
首先你需要做
<input type='submit' />。其次,如果响应状态为 500,则意味着您发送表单的操作遇到了运行时错误。这意味着您需要调试服务器端代码。您的表单实际上正在推送,否则您将无法从服务器获得响应!
标签: javascript html asp.net-mvc razor asp.net-ajax