フルコードを生成する場合は、MvcForm
を返すのは間違いでしょう。 MvcHtmlString
を返し、そのメソッド内でHTMLを構築する必要があります。
@Html.DeleteButton("Delete", "Boodschap", new { id = item.BoodschapID });
直接HTMLを生成:あなたのようにそれを使用することができますその方法(注:テストされていない、あなたは、などを、適切なヌルチェックを必要とするかもしれないが)
public static MvcHtmlString DeleteButton(this HtmlHelper helper, string name,
string actionName, object htmlAttributes)
{
return DeleteButton(helper, name, actionName, null, null, htmlAttributes);
}
public static MvcHtmlString DeleteButton(this HtmlHelper helper, string name,
string actionName, string controllerName, object routeValues,
object htmlAttributes)
{
var buttonBuilder = new TagBuilder("button");
buttonBuilder.SetInnerText(name);
var formBuilder = new TagBuilder("form");
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
formBuilder.Attributes.Add("action", urlHelper.Action(
actionName, controllerName, routeValues))
formBuilder.Attributes.Add("method", FormMethod.Post);
formBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
formBuilder.InnerHtml = buttonBuilder.ToString();
return new MvcHtmlString(formBuilder.ToString());
}
代替は、フォームヘルパーを再利用することであろう私は<form>
要素は、トリックを行うだろうと思いますが、それは非常にAJAX-yのではない、
public static MvcHtmlString DeleteButton(this HtmlHelper helper, string name, string actionName, object routeValues)
{
return DeleteButton(helper, name, actionName, null, routeValues, null);
}
public static MvcHtmlString DeleteButton(this HtmlHelper helper, string name, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
using (helper.BeginForm(actionName, controllerName, routeValues, FormMethod.Post, htmlAttributes))
{
var response = helper.ViewContext.HttpContext.Response;
var builder = new TagBuilder("button");
builder.SetInnerText(name);
response.Write(builder.ToString(TagRenderMode.Normal));
}
return MvcHtmlString.Create("");
}
は私が最後の答えを編集しました。 –
HtmlHelperで 'response.Write'をどうやって行うのか、とても良い見通しです。ありがとう!!私は再びそれを投票することを願っています。 –