I'm new to razor and display templates so please correct me where I'm wrong. I'm working with a string that contains HTML. If I try converting it directly to a HtmlString, my Ajax form refuses to submit. As a workaround I tried to use Display templates with the goal of achieving something like this:
I have a partial view with an ajax form:
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>
}
My @foreach
loop contains this
@Html.DisplayFor(modelItem => myModel.myString, "HtmlString")
My DisplayTemplate contains this
HtmlString.cshtml
@model String
@{
@Html.Raw(Model)
}
So if myString = "<ul><li>my text</li></ul>"
I would like my ajax form to display
- my text
Now the the text displays correctly, but the form refuses to submit. If I were to display the string as normal, the form would submit with no problems. Please help with what I'm doing wrong.
UPDATE: Below is the rendered HTML source of the form