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

例如: 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=,UserName="admin",Name="张三"},
new User{Id=,UserName="pm",Name="张三"},
new User{Id=,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=,UserName="admin",Name="张三"},
new User{Id=,UserName="pm",Name="张三"},
new User{Id=,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=,UserName="admin",Name="张三"},
new User{Id=,UserName="pm",Name="张三"},
new User{Id=,UserName="one",Name="张三"},
};
User u = new User();
u.Id = ;
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(, "admin");
myd.Add(,"pm");
myd.Add(, "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里面的“”很重要!

                         }

             }

MVC 中的家常事的更多相关文章

  1. .NetCore MVC中的路由(2)在路由中使用约束

    p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...

  2. .NetCore MVC中的路由(1)路由配置基础

    .NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...

  3. Asp.Net MVC中使用StreamReader读取“Post body”之应用场景。

    场景:有三个市场(Global.China.USA),对前台传过来的数据有些验证需要细化到每个市场去完成. 所以就出现了基类(Global)和派生类(China.USA) 定义基类(Global)Pe ...

  4. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  5. ASP.NET Core MVC 中的 [Controller] 和 [NonController]

    前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...

  6. ASP.NET MVC中利用AuthorizeAttribute实现访问身份是否合法以及Cookie过期问题的处理

    话说来到上海已经快半年了,时光如白驹过隙,稍微不注意,时间就溜走了,倒是没有那么忙碌,闲暇之际来博客园还是比较多的,记得上次在逛博问的时候看到有同志在问MVC中Cookie过期后如何作相关处理,他在阐 ...

  7. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  8. 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  9. 2.ASP.NET MVC 中使用Crystal Report水晶报表

    上一篇,介绍了怎么导出Excel文件,这篇文章介绍在ASP.NET MVC中使用水晶报表. 项目源码下载:https://github.com/caofangsheng93/CrystalReport ...

随机推荐

  1. 扩展jquery scroll事件,支持 scroll start 和 scroll stop

    效果预览: github: https://besswang.github.io/webapp-scroll/ 参考地址: http://www.ghugo.com/special-scroll-ev ...

  2. 转载 Mixed Content Page

    网站配置了https之后,网页上的百度地图无法正常显示,报错类似于: Mixed Content: The page at 'https://url_1' was loaded over HTTPS, ...

  3. Java构建网站多级菜单功能解决方案

    在网站开发的时候我们会对网站的栏目进行分类,一个栏目可以有多个子分类,一个子分类又可以有分裂,例如:新闻栏目下有每日早报和每日晚报两个栏目,其中每日早报下面又分为上海早报,北京早报,杭州早报,下面是京 ...

  4. Code Forces 644A Parliament of Berland

    A. Parliament of Berland time limit per test1 second memory limit per test256 megabytes inputstandar ...

  5. FZU 2140 Forever 0.5(找规律,几何)

    Problem 2140 Forever 0.5 Accept: 371 Submit: 1307 Special Judge Time Limit: 1000 mSec Memory Limit : ...

  6. Cmake Make makefile GNU autotools

    个人总结 首先makefile是由make来编译,而makefile的生成可以由GUN autotools和CMake来实现,但前者没有CMake的CMakelist.txt直观,所以我们一般用CMa ...

  7. URI 、URL 和 URN

    URI URI 是 Uniform Resource Identifier 的缩写. Uniform 统一不同类型的资源.比如 txt.mp3.jpeg 等不同的类型的资源都可以使用 URI 来标识 ...

  8. CSS cursor 属性

    cursor 1.定义和用法 cursor 属性规定要显示的光标的类型(形状). 该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状(不过 CSS2.1 没有定义由哪个边界确定这个范围). 2 ...

  9. apache2.2版本 configuration error: couldn't perform authentication. AuthType not set!: /

    configuration error:  couldn't perform authentication. AuthType not set!: /  500服务器错误 解决方案: <Dire ...

  10. Linux cal命令

    cal命令时查看日历的相关命令 1.用法 cal [选项] [[[日] 月] 年] 2.命令选项 -1, --one 只显示当前月份(默认) -3, --three  显示上个月.当月和下个月 -s, ...