, , 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() );
}
Response.Write, () , , - :
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("");
}