< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

         HtmlHelper提供了一些帮助的方法返回一个字符串来生成html. 在System.Web.Mvc.Html命称空间下有一些表单,控件,局部视图Helper方法.我将创建一个生成标签<input type=”submit”,名称为SubmitConfirmHelper的类.看下面:

using System.Web.Mvc;
namespace Helpers
{
    public static class SubmitConfirmHelper
    {
        /// <summary> 
        /// Renders an HTML form submit confirm button 
        /// </summary> 
        public static string SubmitConfirm(this HtmlHelper helper, string buttonText, string alertMessage)
        {
            return String.Format("<input type=\"submit\" value=\"{0}\" onClick=\"return confirm('{1}');\" />", buttonText, alertMessage);
        }
    }
}

因为SubmitConfirm()方法扩展自HtmlHelper类,所以这个方法在HtmlHelper下有智能提示 显示出来:

mvc_1

View使用Html.SubmitConfirm()方法来呈现submit 按钮

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 
<%@ Import Namespace="Helpers"%> 
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server"> 
    About Us 
</asp:Content> 
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2>About</h2> 
    <p> 
    <% using (Html.BeginForm()) { %> 
      <%=  Html.SubmitConfirm("Delete", "Do you want to delete?")%> 
    </p> 
     <% } %> 
</asp:Content> 

现在你可以运行下看效果了:

mvc_2

总结

在这篇POST中你学习了如何扩展HTML Helper类来创建自定义HTML


Author: Petter Liu    http://wintersun.cnblogs.com

posted on   PetterLiu  阅读(876)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示