zoukankan      html  css  js  c++  java
  • HtmlHelper2

    一、隐式从ViewBag取数据

    1、action中的代码:

    1 ViewBag.UserName = "admin";

    cshtml中的代码:

    1 @Html.TextBox("UserName")

    这样就会在@Html.TextBox("UserName")所在的位置显示admin了

    2、action中的代码:

    1 Book[] books = new Book[]
    2 {
    3     new Book{Id=1,Name="如鹏网"},
    4     new Book{Id=2,Name="腾讯"},
    5     new Book{Id=3,Name="天猫"}
    6 }
    7 
    8 SelectList slBooks = new SelectList(books,"Id","Name",2);
    9 ViewBag.books = slBooks;

    cshtml中的代码:

    @Html.DropDownList("books")

    这样就会在@Html.DropDownList("books")的位置替换显示为下拉列表了。

    二、强类型视图绑定

    Action:

    1 public ActionResult Index()
    2 {
    3      return View();
    4 }

    Model:

    1 public class UserModel
    2     {
    3         public int Id { get; set; }
    4         public string Name { get; set; }
    5     }

    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

     1 public class UserModel
     2     {
     3         [DisplayName("用户名")]
     4         [Required]
     5         [StringLength(8)]
     6         public string UserName { get; set; }
     7 
     8         [DisplayName("密码")]
     9         [Required]
    10         [StringLength(8)]
    11         public string Password { get; set; }
    12     }

    Controller

    1 public ActionResult Login(UserModel model)
    2         {
    3             if (!ModelState.IsValid)
    4             {
    5                 return View(nameof(Index));//转到Index.cshtml页面
    6             }
    7             return Content(model.UserName);
    8         }

    cshtml:

     1 @model 客户端数据验证.Models.UserModel
     2 @{
     3     ViewBag.Title = "Home Page";
     4 }
     5 
     6 @using (Html.BeginForm("Login", "Home"))
     7 {
     8     @Html.LabelFor(m => m.UserName, "用户名") @Html.TextBoxFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName)<br />
     9     @Html.LabelFor(m => m.Password, "密码") @Html.TextBoxFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password)<br />
    10     <input type="submit" value="提交" />
    11 }

    然后在Layout.cshtml中的head头部引入

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")

    完成以上步骤后,客户端提交表单之前就可以验证了。

  • 相关阅读:
    HDU1720 A+B Coming
    HDU1390 ZOJ1383 Binary Numbers
    HDU1390 ZOJ1383 Binary Numbers
    HDU2504 又见GCD
    HDU2504 又见GCD
    HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking
    HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking
    HDU1020 ZOJ2478 Encoding
    HDU1020 ZOJ2478 Encoding
    HDU2097 Sky数
  • 原文地址:https://www.cnblogs.com/dotnetHui/p/7967635.html
Copyright © 2011-2022 走看看