asp.net mvc 学习(六)

HtmlHelper

一般使用a标签

@* 一般使用标签方式 *@
<a href="/home/about" style="color:black" id="111">去about</a>

Url内置对象

第一种方式

@* 
    有两种方式可以实现a标签的使用
    1:通过内置对象的action()方式实现:
    Url.Action(action的名称,控制器的名称,页面传递值(键值对的方式传递))
    当只填入action的名称的时候,使用当前的控制器进行访问
    
*@
<a href="@Url.Action("about","home",new { name = "tom" })">通过url方式跳转about</a>
<a href="@Url.Action("about")">通过url方式跳转about</a>
<hr />

 第二种方式

  2:使用HtmlHelper辅助类对象Html.ActionLink(链接文本信息,action名称,控制器名称,页面传递的值,为a标签添加的属性)
    Html.ActionLink("通过html跳转到about","about","home",new{name="tom",new{style="color:aqua",id="iii"}}
@Html.ActionLink("通过html跳转about", "about", new { name = "tom" }, new {style="color:yellow",id="iiii"})
@Html.ActionLink("通过html跳转about","about")
@Html.ActionLink("通过html跳转about", "about", "home", new { name = "tom" });


为什么要用这些辅助类

  原因:传统的a标签,一旦路由发生变化,那么系统的所有a标签都要改为路由规定的样式才能访问,但是这样做效率极低,改进的方式使用url或者html提供的方式智能自动组织url,无论路由如何变化,它们都不会受到影响。


文本框

<input id="Text1" type="text" name="username" value="输入姓名" style="color:red" class="danger" />
@Html.TextBox("username", "亲输入姓名", new { style = "color:red", @class = "danger" })/*这时报错,class这,为什么,class是关键字写的c#代码*/
<hr />


文本域

@Html.TextArea("taname", "亲输入内容", 5, 10, new {style="color:blue"})


密码框

@Html.Password("pwd")

 

 


复选框

<hr />
<p>
    性别:
</p>
<p>
    boy:@Html.CheckBox("check1", true, new {style="blue"})
</p>
<p>
    girl:@Html.CheckBox("check1", false, new { style = "blue" })
</p>

 


单选框

<p>
    女:@Html.RadioButton("rb1","1",true)
</p>
<p>
   男:@Html.RadioButton("rb1", "2",true)
</p>

 


 下拉框

@{
    ViewBag.Title = "Home Page";
    List<SelectListItem> list = ViewData["data"] as List<SelectListItem>;
} 
@* 一般使用标签方式 *@
<a href="/home/about" style="color:black" id="111">去about</a>
@*
    有两种方式可以实现a标签的使用
    1:通过内置对象的action()方式实现:
    Url.Action(action的名称,控制器的名称,页面传递值(键值对的方式传递))
    当只填入action的名称的时候,使用当前的控制器进行访问
    2:使用HtmlHelper辅助类对象Html.ActionLink(链接文本信息,action名称,控制器名称,页面传递的值,为a标签添加的属性)
    Html.ActionLink("通过html跳转到about","about","home",new{name="tom",new{style="color:aqua",id="iii"}}

*@
@Html.DropDownList("lucky", list);
  //设置下拉框选项的值
            List<SelectListItem> list = new List<SelectListItem>();
            SelectListItem s1 = new SelectListItem()
            {
                Selected = true,
                Text = "吐鲁番",
                Value = "1"
            };
            SelectListItem s2 = new SelectListItem()
            {
                Selected = true,
                Text = "伊犁",
                Value = "2"
            };
            SelectListItem s3 = new SelectListItem()
            {
                Selected = true,
                Text = "喀什",
                Value = "3"
            };
            list.Add(s1);
            list.Add(s2);
            list.Add(s3);
            ViewData["data"] = list;
            return View();

 

 


提交表单

 public ActionResult About()
        {
            string pwd = Request["ininput"];

            return Content(pwd);
        }

        public ActionResult Contact()
        {
            string pwd = Request.Form["username"];

            return Content(pwd);
        }
<form method="post" action="/home/about" name="myform">
    <input id="Text1" type="text" name="ininput"/>
    <input id="Submit1" type="submit" value="submit" class="btn btn-success" />
</form>
@using (Html.BeginForm("contact", "home", FormMethod.Post, new { name = "myform" }))
            {
                @Html.TextBox("username","请输入内容")
<input id="Submit1" type="submit" value="submit" class="btn btn-success" />
            }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值