代码改变世界

System.Web.Mvc.Html 命名空间小计(转)

2015-02-11 15:23  ko绝缘体  阅读(194)  评论(0编辑  收藏  举报

     最近在看MVC框架,发现这个博文对初学者可能有帮助,故转之。

1,Html.Action    使用指定参数调用指定子操作方法并以 HTML 字符串形式返回结果。

Html.Action()
 <div id="HtmlAction">

        @Html.Action("ActionName");     

        @Html.Action("ActionName", "ControlName");
        
        @{
            object a = null;
            
        }
            @Html.Action("ActionName", a);
       
        @{
            RouteValueDictionary rotevalue = new RouteValueDictionary();
            rotevalue.Add("Key", "Value");
        }

            @Html.Action("ActionName", rotevalue); 
         
        @Html.Action("ActionName", "ControlName", rotevalue); 
      
        @Html.Action("ActionName", "ControlName", "ObjectValue");
   
        </div>
View Code

2,Html.RenderAction    通过Controller中的Action来调用用户控件 允许直接调用某一个Action,并把返回的结果直接显示在当前调用的View中

  Html.RenderPartial     直接将用户空间嵌入到界面上

@{
                Html.RenderAction("ActionName");     //优点  可以呈现不同的PartialView()
                Html.RenderAction("ActionName", "ControlName");

                object b = null;
                Html.RenderAction("ActionName", b);
                RouteValueDictionary rotevalue1 = new RouteValueDictionary();
                rotevalue1.Add("Key","Value");
                Html.RenderAction("ActionName", rotevalue);

                Html.RenderAction("ActionName", "ControlName", rotevalue);

                Html.RenderAction("ActionName", "ControlName", "ObjectValue");
                
                Html.RenderPartial("~/Areas/Common/Views/Shared/UserControl.ascx");   //直接将用户控件嵌套到界面上
                object model = null;
                ViewDataDictionary viewdata = new ViewDataDictionary(); 
                viewdata.Add("Key", "Value");
                Html.RenderPartial("UserControl", model);
                Html.RenderPartial("UserControl", model,viewdata);
            }
View Code

3,Html.Display()      使用字符串来表示要呈现的对象值。

   Html.DisplayFor()  使用模型对象表示要呈现的对象值。

   Html.DisplayForModel()   隐式使用模型表示要呈现的对象值。

@model Azurebrite.Areas.Accounts.Models.Domain.LogOnModel
            @{ 
                //显示  Model.UserName
                Html.Display("UserName");
                Html.DisplayFor(m=>m.UserName); 
                Html.DisplayForModel();   //显示Model的全部字段 
             }
View Code

4,Html.DisplayText() 

 //返回指定表达式所表示对象中的每个属性所对应的 HTML 标记。
                 Html.DisplayText("UserName"); 
                 Html.DisplayTextFor(m => m.UserName); 
View Code

5,Html.Editor()

                Html.Editor("UserName");
                 Html.EditorFor(m=>m.UserName);
                 Html.EditorFor(m => m.UserName);
                Html.EditorForModel();  //返回模型中的每个属性所对应的 HTML input 元素。
View Code

6,Html.BeginForm()

@using (Html.BeginForm("/myformrouteurl"))  //Url
            {
                <!-- form here -->

            }


            @using (Html.BeginRouteForm("DefaultRote"))
            {
                <!-- form here -->

            }

            @using (Html.BeginRouteForm("DefaultRote", FormMethod.Post))
            {
                <!-- form here -->

            }
View Code

7,Html Input

@{
                Html.CheckBox("RememberMe");
                Html.CheckBoxFor(m=>m.RememberMe);
                Html.Hidden("UserName");
                Html.HiddenFor(m=>m.UserName,new {@id="id",@name="name" });  
                Html.Password("Password");
                Html.PasswordFor(m=>m.Password);
                Html.RadioButton("Name","Value");
                Html.RadioButtonFor(m=>m.RememberMe,true, new { @id="radio1",@value="1", @name = "RememberMe" });
                Html.RadioButtonFor(m=>m.RememberMe,false, new { @id="radio2",@value="2", @name = "RememberMe" });
                Html.TextBox("UserName",new {@id="id"});
                Html.TextBoxFor(m=>m.UserName,new {@id="id"});
                
                Html.Label("UserName",new {@id="id",@width="100px"});
                Html.LabelFor(m=>m.UserName,new {@style="width:100px,height:30px"});
                Html.LabelForModel();
                
                
            }
View Code

8,Html.ActionLink()

@{
                Html.ActionLink("LinkText","ActionName"); 
                Html.ActionLink("LinkText","ActionName","ControlName"); 
                Html.ActionLink("LinkText","ActionName","ObjectRoutvalues",new {@id="id",@style="width:100px,color:red"});
           
                Html.RouteLink("LinkText","RouteName");
                
            }
View Code

9,Html.MvcForm()

 @{MvcForm form = Html.BeginForm("ProcessForm", "Home");  
 
                    
             form.EndForm();  
             }
View Code