ASP.NET MVC 4 では、ロード時に HTML をレンダリングするにはどうすればよいですか。この形式でエンコードされた html 文字列を、SQL サーバー データベースに nvarchar(max) 型として保存しています。
<li><li><img id="fbPic" src="http://graph.facebook.com/567818188/picture" style="display: inline; padding-right: 10px;"><b>Bernice Zerafa</b></li><pre class="word-wrap" style="margin-left: 10%;">hellooo</pre></li>
*編集: *上記の文字列は正しくエンコードされていない html であるため、実際には次のように表示されることに注意してください。
<li><li><img id="fbPic" src="http://graph.facebook.com/567818188/picture" style="display: inline; padding-right: 10px;"><b>Bernice Zerafa</b></li><pre class="word-wrap" style="margin-left: 10%;">helloooo </pre></li>
ページをロードすると、順序付けされていないリスト タグに追加されるさまざまな子ノードを持つすべてのリスト アイテムである html 文字列のリストが表示されます。リストはOKを返しています。ただし、ページにそのまま表示されるだけです。つまり、実際の文字列が表示され、html はレンダリングされません。
これは私のコントローラーアクションです:
public ActionResult LoadPosts(int groupId)
{
var postedText = new List<string>();
IEnumerable<Post> posts;
using (CodeShareEntities conn = new CodeShareEntities())
{
posts = conn.Posts.Where(g => g.GroupId == groupId);
foreach (var post in posts)
{
postedText.Add(post.PostData);
}
}
return Json(new { PostedText = postedText });
}
これは、ページの読み込み時の jQuery Ajax 呼び出しです。私のhtml#posts
では空です<ul>
jQuery.ajax({
type: 'POST',
url: '/Groups/LoadPosts',
data: { groupId: grpid },
success: function (postData) {
$.each(postData, function (index, postText) {
**postText = htmlUnencode(postText);**
$("#posts")[0].innerHTML = postText;
//// what can I do here instead of innerHTML to be
//// able to view the html correctly ?
//// append() and insert() methods in JQuery have the same result
//// I found something called @Html.Raw(Json.Encode()) maybe
//// this is relevant here ? How can I use this correctly ?
});
$('#posts').css('background', 'white');
},
traditional: true
});
どんな助けでも大歓迎です!