各ユーザーに対してロック/アンロック機能を切り替えるユーザー名の表の各行にボタンを作成しようとしています。私はAJAXを使いたいので、ページをリロードするたびにすべてのユーザーを取得する必要はありません。そのテーブルにAJAXのアクションリンクを持つことは簡単ですが、ユーザーをロックまたはロック解除した後、コントローラから返されるものについては固まっていました。ちょっとしたハックとして、新しいAJAXアクションリンクのhtmlマークアップである文字列を返します。私は理論的に返されたボタンをクリックして、ロック/アンロックをトグルし続けます。私の驚きには、実際にはうまくいきました。ダイナミックボタンは正しいマークアップを返しますが、空白のページに戻ります。さらに問題を複雑にするために、カスタムヘルパーを使用してアクションリンクを出力しています。私は以下のすべてのコードを詳しく説明しました。もし誰かが間違っているとか、このような状況に対処するためのよりよい方法を見ていただければ、私は感謝しています。コントローラアクションからAJAXアクションリンクを動的に返します。
HTMLヘルパー:
public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string altText, string actionName, object routeValues, AjaxOptions ajaxOptions)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", imageUrl);
builder.MergeAttribute("alt", altText);
var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions);
return link.ToHtmlString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
}
コントローラー:
public string Lock(Guid id)
{
IUserMethods userMethods = new UserMethods();
ISMPUser user = userMethods.GetUser(id, CompanyId);
string ajaxButtonHTML;
//For some reason breaking the button HTML into substrings and appending them together for readability causes the anchor tag to render incorrectly.
if (user.IsEnabled)
{
userMethods.AdministratorEnableAccount(CompanyId, CurrentUser.Id, user.Username, false);
ajaxButtonHTML = "<a class=\"row_selected\" href=\"/MMWeb/Admin/Lock/" + id.ToString() + "\" onclick=\"Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, confirm: 'Lock User?', httpMethod: 'Post', updateTargetId: 'Enable-'" + user.Id + "' });\"><img src=\"/MMWeb/Content/Images/lock.png\" alt=\"Lock\"></a>";
}
else
{
userMethods.AdministratorEnableAccount(CompanyId, CurrentUser.Id, user.Username, true);
ajaxButtonHTML = "<a class=\"row_selected\" href=\"/MMWeb/Admin/Lock/" + id.ToString() + "\" onclick=\"Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, confirm: 'Lock User?', httpMethod: 'Post', updateTargetId: 'Enable-'" + user.Id + "' });\"><img src=\"/MMWeb/Content/Images/unlock.png\" alt=\"Unlock\"></a>";
}
return ajaxButtonHTML;
}
ビュー:
<td id="<%= Html.Encode("Enable-" + user.Id) %>" class="icon-column">
<% if(user.IsEnabled)
{ %>
<%--<img class="LockImg" alt="User Unlocked" src="<%= Url.Content("~/Content/Images/unlock.png") %>" />--%>
<%= Ajax.ImageActionLink(Url.Content("~/Content/Images/unlock.png"), "Lock", "Lock", new { id = user.Id.ToString() }, new AjaxOptions { Confirm = "Lock User?", HttpMethod = "Post", UpdateTargetId = "Enable-" + user.Id })%>
<% }
else
{%>
<%= Ajax.ImageActionLink(Url.Content("~/Content/Images/lock.png"), "Lock", "Lock", new { id = user.Id.ToString() }, new AjaxOptions { Confirm = "Unlock User?", HttpMethod = "Post", UpdateTargetId = "Enable-" + user.Id })%>
<% }%>
</td>