MVC2 中某些控件的用法

一MVC2的view页面中form的用法: <% using (Html.BeginForm("方法名称", "控制器名称", FormMethod.Post))  {%>  

 

视图把数据传到控制器的2种方法:

例:   视图代码

    <% using (Html.BeginForm("About","Home"))
       { %>
    <%=Html.TextBox("Name")%>
    <input type="submit" value="submit" name="submit" />
    <%}%>

  控制器代码

(1)   public ActionResult About(FormCollection form)
   {
            string s = form["Name"];
            return View();
  }

(2)public ActionResult About()
   {
            string s = Request.Form["Name"] ;       

            return View();
  }

三 Html.ActionLink的用法

以下使用参数文字说明:

  1. linkText:生成的链接所显示的文字         类型:string
  2. actionName:对应控制器的方法          类型:string
  3. routeValues:向对应的action传递的参数     类型:object 或 RouteValueDictionary
  4. controlName:指定控制器的名称          类型:string
  5. htmlAttributes:设置<a>标签的属性                   类型:object 或 IDictionary
  6. protocol:指定访问协议如:http等        类型:string
  7. hostName:指定访问域名            类型:string
  8. fragment:指定访问锚点             类型:string

重载一:Html.ActionLink("linkText","actionName")【默认当前页面控制器】

调用:<%: Html.ActionLink("默认当前页面控制器", "About")%>

生成效果:<a href="/Home/About">默认当前页面控制器</a> 

重载二:Html.ActionLink("linkText","actionName",routeValues)

调用:  routeValues Is object:

  <%: Html.ActionLink("默认当前页面控制器", "About", new { id = 1, type = "Dic" })%>

  routeValues Is RouteValueDictionary:

  <%RouteValueDictionary Dictionary = new RouteValueDictionary();
  Dictionary["id"] = 1;
  Dictionary["type"] = "Dic";
  %>

  <%: Html.ActionLink("默认当前页面控制器", "About", Dictionary)%>

生成效果:<a href="/Home/About?classid=1">默认当前页面控制器</a> 

重载三:Html.ActionLink("linkText","actionName","controlName")

调用:<%: Html.ActionLink("默认当前页面控制器", "About", "Home")%>

生成效果:<a href="/Home/About">默认当前页面控制器</a> 

重载四:Html.ActionLink("linkText","actionName",routeValues,htmlAttributes)

调用:  htmlAttributes Is object:

  <%: Html.ActionLink("首页", "Index", "Home", null, new { @class = "active", target = "_blank" })%>【注:由于class是保留关键字,所以一定要写成@class】

  htmlAttributes Is IDictionary:

  <%IDictionary<string, object> AttrDictionary = new Dictionary<string, object>();

  AttrDictionary["class"] = "active";

  AttrDictionary["target"] = "_blank";
  %>

生成效果:<a class="active" href="/" target="_blank">首页</a>

 

重载五:Html.ActionLink("linkText","actionName","controlName","protocol","hostName","fragment",routeValues,htmlAttributes)

调用:<%: Html.ActionLink("关于我们", "About", "Home", "http", "localhost", "top", null, null)%>

生成效果:<a href="http://localhost:12120/Home/About#top">关于我们</a>

 

1 DropdownList用法
(1) Controller 控制器中代码
public  SelectList GetList()
        {

            List<SelectListItem> itemList = new List<SelectListItem>();                     
            List<Test> testList = tableTest.Test.ToList();
            SelectListItem item;
            foreach(Test t in testList)
            {
                item = new SelectListItem() {Text=t.name,Value=t.id.ToString()};               
                itemList.Add(item);
            }
            SelectList List = new SelectList(itemList,"value","text");
            return List;
        }
    
        
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult TestEdit(int id)
        {
            Test t = tableTest.Test.First(m => m.id == id);
            ViewData["List"] = GetList();
            return View(t);
        }
(2)视图页面(View)代码
 <div class="editor-field">
                <%= Html.DropDownListFor(model => model.age,ViewData["List"] as SelectList)%>
                <%= Html.ValidationMessageFor(model => model.age)%>
            </div>

 

2 RadioButton用法:

 <%=Html.RadioButtonFor(m=>m.age,0,new{@id = "radio1", @name = "rdolstState",@checked=true }) %>
                <%=Html.RadioButtonFor(m => m.age, 1, new

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值