自定义 Ajax 帮助程序未命中 MVC 中的控制器
Custom Ajax helper does not hit the controller in MVC
我找到了一个很好的 exemple of Ajax 搜索文本框助手,它看起来像这样:
public static MvcHtmlString Textbox(this AjaxHelper ajaxHelper, string name,
AjaxOptions ajaxOptions, object htmlAttributes)
{
var tag = new TagBuilder("input");
tag.MergeAttribute("name", name);
tag.MergeAttribute("type", "text");
tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
从这个例子开始,我正在尝试定制 "AjaxButton",直到现在我有了这个助手:
public static MvcHtmlString AjaxButton(this AjaxHelper ajaxHelper, int jobId, bool apply)
{
var myAjaxOptions = new AjaxOptions()
{
Url = "JobCardView/UserApply"+jobId,
HttpMethod = "post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "userInfo",
OnSuccess = "CallBackCardView"
};
var tag = new TagBuilder("button");
var color = apply ? "warning" : "primary";
tag.MergeAttribute("class", "btn btn-"+color);
tag.MergeAttribute("data-dismiss", "modal");
var text = apply ? "Not Interested" : "Interested";
tag.SetInnerText(text);
tag.MergeAttributes((myAjaxOptions).ToUnobtrusiveHtmlAttributes());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
呈现以下 Html:
<button
class="btn btn-warning"
data-ajax="true"
data-ajax-method="post"
data-ajax-mode="replace"
data-ajax-success="CallBackCardView"
data-ajax-update="#userInfo"
data-ajax-url="JobCardView/UserApply2"
data-dismiss="modal">
Not Interested
</button>
如何在这个助手中提供 Url,以便 运行 如预期的那样?
我有 Ajax.ActionLink
,它具有相同的选项并且 运行 没问题。
而不是对 url 进行硬编码(并且忘记在 jobId
参数前面附加 /
):
Url = "JobCardView/UserApply"+jobId
尝试使用框架内置的工具来处理 urls (UrlHelper
):
Url = new UrlHelper(ajaxHelper.ViewContext.RequestContext).Action("UserApply", "JobCardView", jobId)
这样你就能得到正确的 url:
data-ajax-url="JobCardView/UserApply/2"
我找到了一个很好的 exemple of Ajax 搜索文本框助手,它看起来像这样:
public static MvcHtmlString Textbox(this AjaxHelper ajaxHelper, string name,
AjaxOptions ajaxOptions, object htmlAttributes)
{
var tag = new TagBuilder("input");
tag.MergeAttribute("name", name);
tag.MergeAttribute("type", "text");
tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
从这个例子开始,我正在尝试定制 "AjaxButton",直到现在我有了这个助手:
public static MvcHtmlString AjaxButton(this AjaxHelper ajaxHelper, int jobId, bool apply)
{
var myAjaxOptions = new AjaxOptions()
{
Url = "JobCardView/UserApply"+jobId,
HttpMethod = "post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "userInfo",
OnSuccess = "CallBackCardView"
};
var tag = new TagBuilder("button");
var color = apply ? "warning" : "primary";
tag.MergeAttribute("class", "btn btn-"+color);
tag.MergeAttribute("data-dismiss", "modal");
var text = apply ? "Not Interested" : "Interested";
tag.SetInnerText(text);
tag.MergeAttributes((myAjaxOptions).ToUnobtrusiveHtmlAttributes());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
呈现以下 Html:
<button
class="btn btn-warning"
data-ajax="true"
data-ajax-method="post"
data-ajax-mode="replace"
data-ajax-success="CallBackCardView"
data-ajax-update="#userInfo"
data-ajax-url="JobCardView/UserApply2"
data-dismiss="modal">
Not Interested
</button>
如何在这个助手中提供 Url,以便 运行 如预期的那样?
我有 Ajax.ActionLink
,它具有相同的选项并且 运行 没问题。
而不是对 url 进行硬编码(并且忘记在 jobId
参数前面附加 /
):
Url = "JobCardView/UserApply"+jobId
尝试使用框架内置的工具来处理 urls (UrlHelper
):
Url = new UrlHelper(ajaxHelper.ViewContext.RequestContext).Action("UserApply", "JobCardView", jobId)
这样你就能得到正确的 url:
data-ajax-url="JobCardView/UserApply/2"