HtmlHelper2
一、隐式从ViewBag取数据
1、action中的代码:
- ViewBag.UserName = "admin";
cshtml中的代码:
- @Html.TextBox("UserName")
这样就会在@Html.TextBox("UserName")所在的位置显示admin了
2、action中的代码:
- Book[] books = new Book[]
- {
- new Book{Id=,Name="如鹏网"},
- new Book{Id=,Name="腾讯"},
- new Book{Id=,Name="天猫"}
- }
- SelectList slBooks = new SelectList(books,"Id","Name",);
- ViewBag.books = slBooks;
cshtml中的代码:
- @Html.DropDownList("books")
这样就会在@Html.DropDownList("books")的位置替换显示为下拉列表了。
二、强类型视图绑定
Action:
- public ActionResult Index()
- {
- return View();
- }
Model:
- public class UserModel
- {
- public int Id { get; set; }
- public string Name { get; set; }
- }
cshtml:
- @model 强类型视图绑定.Models.UserModel
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- @Html.Label("Id"):@Html.TextBoxFor(m => m.Id)<br />
- @Html.Label("Name"):@Html.TextBoxFor(m => m.Name)<br />
- </div>
- </body>
- </html>
三、数据客户端验证
建一个MVC模板的MVC项目。
Model
- public class UserModel
- {
- [DisplayName("用户名")]
- [Required]
- [StringLength()]
- public string UserName { get; set; }
- [DisplayName("密码")]
- [Required]
- [StringLength()]
- public string Password { get; set; }
- }
Controller
- public ActionResult Login(UserModel model)
- {
- if (!ModelState.IsValid)
- {
- return View(nameof(Index));//转到Index.cshtml页面
- }
- return Content(model.UserName);
- }
cshtml:
- @model 客户端数据验证.Models.UserModel
- @{
- ViewBag.Title = "Home Page";
- }
- @using (Html.BeginForm("Login", "Home"))
- {
- @Html.LabelFor(m => m.UserName, "用户名") @Html.TextBoxFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName)<br />
- @Html.LabelFor(m => m.Password, "密码") @Html.TextBoxFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password)<br />
- <input type="submit" value="提交" />
- }
然后在Layout.cshtml中的head头部引入
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
完成以上步骤后,客户端提交表单之前就可以验证了。
HtmlHelper2的更多相关文章
- Asp.net MVC 简单分页 自做简单分页
Asp.net MVC 简单分页: public static string Pager(int page,int pageSize,int total) { ...
随机推荐
- POST提交数据之---Content-Type的理解
Content-Type是指http/https发送信息至服务器时的内容编码类型,contentType用于表明发送数据流的类型,服务器根据编码类型使用特定的解析方式,获取数据流中的数据. 在网络请求 ...
- JMeter设置响应数据的编码格式
1.修改配置文件jmeter.properties第974行,默认编码格式为ISO-8859-1,手动修改为UTF-8 2.增加元器件 在线程组右键,添加->后置处理器->BeanShel ...
- python_django_models模块
django中models模块为各类数据库提供了统一的api,可根据不同的业务需求配置数据库. models模块开发流程: 配置数据库 详情:https://www.cnblogs.com/Vera ...
- [JZOJ3235] 数字八
题目 题目大意 给你一个二维的图,其中.代表完好,*代表有缺陷. 现在要在图上刻一个数字\(8\),满足: 由两个矩形组成. 每个矩形中必须有空隙在内部,也就是说,至少为\(3*3\)的矩形. 上矩形 ...
- kmp变形,带通配符的kmp——华科校赛E 好题
https://blog.csdn.net/a302549450/article/details/80948741?tdsourcetag=s_pctim_aiomsg 上面是题解的链接.., 其实和 ...
- Iview+Vue CDN NetMvC 简单demo
1.引用相关js文件 2.菜单采用静态数据加载 3.效果展示 4.代码下载 https://github.com/sulin888/NetVueAdmin.git
- python相关软件安装流程图解——MySQL 8.0.13安装教程(windows 64位)——MYSQL依赖的软件——MYSQL必须的系统DLL插件——MYSQL真正的安装
https://www.mysql.com/https://www.mysql.com/downloads/https://dev.mysql.com/downloads/windows/https: ...
- iOS逆向系列-Reveal
概述 Reveal是一款调试iOS程序UI界面的神器. 官网地址:https://revealall.com 下载:https://revealapp.com/download/ 建议下载Reveal ...
- windows 内核下获取进程路径
windows 内核下获取进程路径 思路:1):在EPROCESS结构中获取.此时要用到一个导出函数:PsGetProcessImageFileName,申明如下: NTSYSAPI UCHAR * ...
- Python全栈开发:协程代码实例
协程代码1 #!/usr/bin/env python # -*- coding;utf-8 -*- # 导入协程模块 """ 协程工作原理 ""&q ...