ASP.NET MVC MESS

ASP.NET MVC Html.Action()

http://www.2cto.com/kf/201207/143417.html

ASP.NET MVC Html.TextBox 与 Html.TextBoxFor 区别

For支持强类型(model的类型),内部使用泛型实现

http://topic.csdn.net/u/20100223/23/47046e86-884d-4a68-ab81-c2943e6fdf98.html

http://stackoverflow.com/questions/5119373/difference-between-html-textbox-and-html-textboxfor

http://www.cnblogs.com/zhangziqiu/tag/ASP.NET%20MVC/

http://www.cnblogs.com/zhangziqiu/archive/2009/03/11/Aspnet-MVC-3.html

只有PartialViewResult 和 ViewResult 才会去寻找View对象.其他的ActionResult要么是返回文件, 要么是跳转等等

寻找Controller和Action是通过路由  寻找页面是通过视图引擎

http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/Aspnet-MVC-4.html

在Action中向View传递Model的几种方式.以及View获取Model以后如何编写显示逻辑.还详细的介绍了ASP.NET MVC框架提供的Html Helper类的使用及如何为Html Helper类添加自定义扩展方法

虽然ViewData和TempData都可以传递弱类型数据,ViewData的生命周期和View相同, 只对当前View有效,TempData保存在Session中, Controller每次执行请求的时候会从Session中获取TempData并删除Session, 获取完TempData数据后虽然保存在内部的字典对象中,但是TempData集合的每个条目访问一次后就从字典表中删除. 也就是说TempData的数据至多只能经过一次Controller传递.

传递强类型对象

public ActionResult StrongTypedDemo()

{

  StrongTypedDemoDTO model = new StrongTypedDemoDTO() { UserName="ziqiu.zhang", UserPassword="123456" };

  return View(model);

}

遍历ViewData集合

Action

 public ActionResult ShowModelWithInsideCodeDemo()

 {

   ViewData["k1"] = @"<script type=""text/javascript"">";

   ViewData["k2"] = @"alert(""Hello ASP.NET MVC !"");";

   ViewData["k3"] = @"</script>";

   return View("ShowModelWithInsideCode");

 }

View

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>使用内嵌代码输出ViewData</title>

    <% foreach(KeyValuePair<string, object> item in ViewData )

       {%>

            <% = item.Value %>

    <% } %>    

</head>

<body>

    <div>   

        <div>此页面运行的脚本代码为:</div>

        <fieldset>       

        <% foreach(KeyValuePair<string, object> item in ViewData )

           {%>

               <% = Html.Encode(item.Value) %> <br />

        <%  } %>

        </fieldset> 

    </div>

</body>

</html>

页面上遍历了两遍ViewData,第一次是作为脚本输出的, 第二次由于进行了HTML编码,所以将作为文字显示在页面上.

在ViewPage中提供了Html属性, 这就是一个HtmlHelper类的实例.如何使用HtmlHelper类:

 <div>

   <% using (Html.BeginForm())

     { %>

    <label style="width:60px;display:inline-block;">用户名:</label>

   <% =Html.TextBox("UserName", "ziqiu.zhang", new { style="width:200px;" })%>

     <br /><br /> 

    <label style="width:60px;display:inline-block;">密码:</label>

   <% =Html.Password("Psssword", "123456", new { style = "width:200px;" })%>        <% }%>

 </div>

----------------------------------------------------------------------------------------------------------------------------------

下拉框 

//所属下拉框

 IEnumerable<TenantType> tenantTypes = tenantTypeService.Gets("site");

 //添加一个自定义项

 List<TenantType> tenantTypesList = tenantTypes.ToList<TenantType>();

 tenantTypesList.Insert(0, new TenantType { ApplicationId = 0, Name = "不限", TenantTypeId = "" });

 SelectList tenants = new SelectList(tenantTypesList.Select(n => new { text = n.Name, value = n.TenantTypeId }), "value", "text", TagTenantType);

 ViewData["tenants"]=tenants;

下拉对象的第一个参数需要是有俩个以上属性的的对象构成的集合,第三个参数是指定取第一个参数的哪个属性做下拉值,第四个参数是指定取第一个参数的哪个属性做下拉文本,最后一个参数指定选中哪个项

 只添加一项

SelectList groupList= new SelectList(new List<SelectListItem>() { new SelectListItem() { Text = "无分组", Value = "" } },"value","text");

在原有项的基础上添加一项默认项

@Html.DropDownList("TenantTypeId", tenants,"不限", new { @class = "tn-dropdownlist tn-input-medium" })

DropDownList的第一个参数名,不能 与ViewData的键名相同 否则将不能选中指定的项

在cshtml中,可以这样获取路由里的某些值 如areaName

     RouteValueDictionary routeValueDictionary = Html.ViewContext.RouteData.DataTokens;

     string areaName = routeValueDictionary.Get<string>("area", string.Empty);

在controller里

RouteValueDictionary routeValueDictionary = Request.RequestContext.RouteData.DataTokens;

 string areaName = routeValueDictionary.Get<string>("area",null);

给Partial页传Model及其他值 

@Html.Partial("_List", Model, ViewData)

Url.Action()生成请求的action的url

'@Html.Raw(Url.Action("_PhotoList", new { spaceKey = Url.SpaceKey(), albumId = albumId }))'

ASP.NET MVC里如果局部页面不是异步加载的,那它就真的是跟这个大页面"一起的",它当然也是这个大页面的一部分,比如

@Html.Action("_AnswerContent", "ChannelAsk")虽然没有给Action传值,但如果此时大页面如果有查询字符串answerId=1,那么这个局部页面对应的Action 

Public ActionResult _AnswerContent(long answerId){}也会接收到这个参数

而,如果一个局部页面是通过js异步加载到这个页面的,那么它不是真的是跟这个大页面"一起的",虽然它显示出来是这个大页面的一部分,比如

上面相同情况,这个通过异步加载过来的页面对应的Action 

Public ActionResult _AnswerContent(long answerId){}则不会接收到这个参数

猜你喜欢

转载自kendezhu.iteye.com/blog/1610174