最近的项目是用asp.net MVC开发的,所以希望从实际开发的角度,通过一些实例,帮助大家快速的开发asp.net MVC程序。
1.创建控件,MVC中通过htmlHelper生成HTML标签。
最后一项htmlAttributes,可以设置样式等属性。
2.RegisterRoutes带来了什么变化。通过VS创建一个MVC应用程序,会在Global.asax中看到下面的代码(我添加了第一条)
现在看一下它带来的变化,假设view有如下代码
或
对应的Url应该是 localhost:XXXX/Account/List/Man 和 localhost:XXXX/Account/List/Woman
当然也可以不在Global.asax中注册那条规则,对应的Url就会变成大家熟悉的 localhost:XXXX/Account/List?type=Man。
顺便提一下我在开发中遇到过的一个问题,还以上面的例子为例,在程序的开发阶段,没有加入刚才那条规则,当我们在程序中加入了 sitemap(mvcsitemap),结果却是sitemap的路径永远不能指向Woman的路径,可能是sitemap不能通过 ?后面参数区分路径,后来加入了这条规则,url中去掉参数,问题解决了。
3.ajax异步请求controller
controller中有如下代码
view中代码如下
4.扩展htmlHelper,方便在view中调用后台代码。
步骤 1)创建静态类 HtmlHelperExtension。
2)引入 System.Web.Mvc 。
3)创建需要的方法,例如:
4)在view中引入HtmlHelperExtension所在的命名空间(一定不要忘,我忘了很多次)
5)在view中使用,例如 <%= Html.FirstExtension() %>
就先说这么多,有些已经记不太清了,错误的地方或说的不全面的地方,希望大家指正与补充。
1.创建控件,MVC中通过htmlHelper生成HTML标签。
1 <%= Html.TextBox("UserName")%>
2 <%= Html.TextBox("UserName","Coolin")%>
3 <%= Html.TextBox("UserName","Coolin",
new { @class = "className",disabled = true })%>
2 <%= Html.TextBox("UserName","Coolin")%>
3 <%= Html.TextBox("UserName","Coolin",
new { @class = "className",disabled = true })%>
最后一项htmlAttributes,可以设置样式等属性。
2.RegisterRoutes带来了什么变化。通过VS创建一个MVC应用程序,会在Global.asax中看到下面的代码(我添加了第一条)
1 routes.MapRoute(
2 "Account",
3 "Account/List/{type}",
4 new { controller = "Account", action = "List" }
5 );
6
7 routes.MapRoute(
8 "Default",
9 "{controller}/{action}/{id}",
10 new { controller = "Home", action = "Index", id = "" }
11 );
2 "Account",
3 "Account/List/{type}",
4 new { controller = "Account", action = "List" }
5 );
6
7 routes.MapRoute(
8 "Default",
9 "{controller}/{action}/{id}",
10 new { controller = "Home", action = "Index", id = "" }
11 );
现在看一下它带来的变化,假设view有如下代码
1 <%= Html.ActionLink("男性", "List", new { type = "Man" }) %>
2 <%= Html.ActionLink("女性", "List", new { type = "Woman" }) %>
2 <%= Html.ActionLink("女性", "List", new { type = "Woman" }) %>
或
1 <%= Url.RouteUrl("Account", new { controller = "Account", action = "List", type= "Man" }) %>
2 <%= Url.RouteUrl("Account", new { controller = "Account", action = "List", type= "Woman" }) %>
2 <%= Url.RouteUrl("Account", new { controller = "Account", action = "List", type= "Woman" }) %>
对应的Url应该是 localhost:XXXX/Account/List/Man 和 localhost:XXXX/Account/List/Woman
当然也可以不在Global.asax中注册那条规则,对应的Url就会变成大家熟悉的 localhost:XXXX/Account/List?type=Man。
顺便提一下我在开发中遇到过的一个问题,还以上面的例子为例,在程序的开发阶段,没有加入刚才那条规则,当我们在程序中加入了 sitemap(mvcsitemap),结果却是sitemap的路径永远不能指向Woman的路径,可能是sitemap不能通过 ?后面参数区分路径,后来加入了这条规则,url中去掉参数,问题解决了。
3.ajax异步请求controller
controller中有如下代码
1 public ActionResult Delete(Guid id)
2 {
3 Delete(id);
4 return Json(new { success = true });
5 }
2 {
3 Delete(id);
4 return Json(new { success = true });
5 }
view中代码如下
1 $.getJSON(<%= Url.Action("Delete", "Account",new { id="xx-xx-xxx" }) %>,
2 function(result) {
3 if (result.success) {
4 //通过脚本移除此行
5 alert("删除成功!")
6 }
7 });
2 function(result) {
3 if (result.success) {
4 //通过脚本移除此行
5 alert("删除成功!")
6 }
7 });
4.扩展htmlHelper,方便在view中调用后台代码。
步骤 1)创建静态类 HtmlHelperExtension。
2)引入 System.Web.Mvc 。
3)创建需要的方法,例如:
1 public static string FirstExtension(this HtmlHelper htmlHelper)
2 {
3 return "FirstExtension";
4 }
2 {
3 return "FirstExtension";
4 }
4)在view中引入HtmlHelperExtension所在的命名空间(一定不要忘,我忘了很多次)
5)在view中使用,例如 <%= Html.FirstExtension() %>
就先说这么多,有些已经记不太清了,错误的地方或说的不全面的地方,希望大家指正与补充。
分类:
mvc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?