MVC 中的家常事

       对于@后面变量加上括号解决异意的方法 

                 例如: hello@User.Name 会误判为电子邮箱,解决办法是括号,即hello@(User.Name)

       使用@Html.Raw()输出后台到.cshtml的前端代码

                  如后台               ViewBag.abc="<a href=\"#\">123</a>";

                  .cshtml              @Html.Raw(ViewBag.abc);

       Head中的可变模板<head>@RenderSection(“onehead”,false) </head>   在继承该模板的页面使用时:@section onehead{}

        一些基本的东西还是整一下吧,不然老百度也不是事,好记性不如烂笔头,写一写吧

          @Html.ActionLink()的两种重载,会用了其他 的就不用说了

           @Html.ActionLink("删除链接", "MyHome", "Home", new {},new { id="myid", @class="myclass", onclick="return confirm(\"你确定要删除吗?\")"})

                                     // 生成结果如下,如果参可传也应像上面一样传第一个New

                                     <a class="myclass" id="myid" onclick="return confirm("你确定要删除吗?")" href="/Home/MyHome">删除链接</a>

             // 下面这个New是传的参数,第二个New传的是HTML属性

          @Html.ActionLink("删除链接", "MyHome", "Home", new {id="123",name="张三"},new { id="myid", @class="myclass", onclick="return confirm(\"你确定要删除吗?\")"}) 

                               //结果下面,上面的第一个New是传的参数,第二个New传的是HTML属性

                             <a class="myclass" id="myid" onclick="return confirm("你确定要删除吗?")" href="/Home/MyHome/123?name=%E5%BC%A0%E4%B8%89">删除链接</a>

          @Html.ActionLink("删除链接", "MyHome", "Home")

            

 @Html.BeginForm()

            //  指定Form表单提交方式和路径等

           @using (Html.BeginForm("MyForm", "FormController", FormMethod.Post))
            {

 

}

 <form action="/FormController/MyForm" method="post"/>    // 结果

// 下面是带 HTML标签id和class属性的
@using (Html.BeginForm("MyForm", "FormController", FormMethod.Post, new { id="123",@class="myclass"}))
{

 

}

<form class="myclass" id="123" action="/FormController/MyForm" method="post"/>   // 结果

//下面是带参数的见下面的new
@using (Html.BeginForm("Myform", "FormController", new { Myid = "123", name = "张三" }, FormMethod.Post))
{

 

}

<form action="/FormController/Myform?Myid=123&name=%E5%BC%A0%E4%B8%89" method="post"/>    // 结果

//  下面是带参数和HTML属性的,第一个New传的是参数,第二个new 传的HTML是属性
@using (Html.BeginForm("MyForm", "FormController", new { myid = "123", name = "张三" }, FormMethod.Post, new { id = "myform", @class = "myclass" }))
{

 

}

<form class="myclass" id="myform" action="/FormController/MyForm?myid=123&name=%E5%BC%A0%E4%B8%89" method="post"/>   // 结果

 

 

@Html.TextBox()和@Html.TextBoxFor()

@Html.TextBox("myText")
<input id="myText" name="myText" type="text" value="" />  // 结果

@Html.TextBox("mytxt", Model.Name, new { @style = "color:Red;" })
<input id="mytxt" name="mytxt" type="text" style=" color:Red" value="Model.Name的值" />   // 结果

@Html.TextBoxFor(m => m.Name, new { @style=" color: Red;"})
<input id="name" type="text" name="name" style="color: Red;" />  // 结果

   如果与后台相关 可在后台实体中[Display(Name="用户名:")]  ,那个 @Html.LableFor(m=>m.UserName)  则显示为用户名 ,代码如下:

namespace MvcApplication1.Models
{
    public class User
    {
        public int Id { get; set; }

        public string UserName { get; set; }

        [Display(Name="用户真实姓名")]
        public string Name { get; set; }
    }
}

在前台View中的代码

@{
    ViewBag.Title = "MyHome";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model MvcApplication1.Models.User

@Html.LabelFor(m=>m.Name)
@Html.TextBoxFor(m=>m.Name)

显示结果

      

  注意上面 @Html.LabelFor的翻译

 

 @Html.CheckBox()

           @Html.CheckBox("chk",true)

            @Html.CheckBoxFor(m=>m.IsVaild,new {@class="myclass"}) 

 

 

@Html.DropDownList("ddl1",(SelectList)ViewData["TName"],"--XXx--")

         

      public ActionResult Index()
        {
            List<User> list = new List<User>(){
            new User{Id=1,UserName="admin",Name="张三"},
            new User{Id=2,UserName="pm",Name="张三"},
            new User{Id=3,UserName="one",Name="张三"},
            };
            ViewData["listUser"] = new SelectList(list, "Id", "UserName");
            return View();
        }





@Html.DropDownList("ddlUserName", ViewData["listUser"] as SelectList,"---请选择-")

 

      强类型的如下

        public ActionResult Index()
        {
            List<User> list = new List<User>(){
            new User{Id=1,UserName="admin",Name="张三"},
            new User{Id=2,UserName="pm",Name="张三"},
            new User{Id=3,UserName="one",Name="张三"},
            };
            ViewData["listUser"] = new SelectList(list, "Id", "UserName");
            User u = new User();
            u.UserName = "admin";
            return View(u);
        }



@Html.DropDownListFor(m => m.Id, ViewData["listUser"] as SelectList, "请选择", new { @class="myclass"})

强类型选中如下: 选中u.id=2的

        public ActionResult Index()
        {
            List<User> list = new List<User>(){
            new User{Id=1,UserName="admin",Name="张三"},
            new User{Id=2,UserName="pm",Name="张三"},
            new User{Id=3,UserName="one",Name="张三"},
            };
            User u = new User();
            u.Id = 2;
            ViewData["listUser"] = new SelectList(list, "Id", "UserName",u.Id);
            u.UserName = "pm";
            return View(u);
        }


@Html.DropDownListFor(m => m.Id, ViewData["listUser"] as SelectList, new { @class="myclass"})    // 

 

数据字典类型转换

        public ActionResult MyHome()
        {
            Dictionary<int, string> myd = new Dictionary<int, string>();
            myd.Add(1, "admin");
            myd.Add(2,"pm");
            myd.Add(3, "one");
            ViewData["ddl"] = new SelectList(myd, "Key", "Value");
            return View();
        }


@Html.DropDownList("ddl",(SelectList)ViewData["ddl"],"---selectedone-")

 

@Html.RadioButton()

   

     一组单选筐须id和name相同,代码如下

             

@Html.RadioButton("rdSix","",true)
@Html.RadioButton("rdSix","")

 

         @{

                     if(ViewBag.IsError!=null && ViewBag.IsError )

                         {

                               <script> alert("@ViewBag.Message")  </script>   // 注意alert里面的“”很重要!

                         }

             }

 

 

 

posted @ 2014-07-09 22:44  酒沉吟  阅读(209)  评论(0编辑  收藏  举报