ASP.NET MVC5网站开发之用户添加和浏览2(七)

yizhihongxing

《ASP.NET MVC5网站开发之用户添加和浏览2(七)》是一篇系列文章中的一篇,主要介绍了如何在ASP.NET MVC5网站中实现用户添加和浏览功能。该文章主要分为以下几部分:

  1. 实现用户添加功能。
  2. 实现用户浏览功能。
  3. 使用Bootstrap样式美化界面。

具体攻略如下:

  1. 实现用户添加功能

步骤如下:

  • 在MVC项目的Controller文件夹下创建UserController.cs文件,设置如下代码:
using System.Web.Mvc;
using WebApplication.Models;

namespace WebApplication.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(User userModel)
        {
            if (ModelState.IsValid)
            {
                // 将用户信息存入数据库
                return RedirectToAction("Index", "Home");
            }
            return View(userModel);
        }
    }
}
  • 在Models文件夹下创建User.cs文件,设置如下代码:
using System.ComponentModel.DataAnnotations;

namespace WebApplication.Models
{
    public class User
    {
        [Key]
        public int Id { get; set; }

        [Required(ErrorMessage = "Please enter your name")]
        [StringLength(50)]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter your email")]
        [DataType(DataType.EmailAddress)]
        [EmailAddress(ErrorMessage = "Please enter a valid email address")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Please enter your password")]
        [DataType(DataType.Password)]
        [StringLength(100, MinimumLength = 6, ErrorMessage = "Password length must be at least 6 characters")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Compare("Password", ErrorMessage = "Passwords do not match")]
        public string ConfirmPassword { get; set; }
    }
}
  • 在MVC项目的Views文件夹下创建User文件夹,并在该文件夹下创建Register.cshtml文件,设置如下代码:
@model WebApplication.Models.User

@{
    ViewBag.Title = "Register";
}

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Register" class="btn btn-default" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

示例说明:

在注册页面中,使用了Html.EditorFor和Html.ValidationMessageFor等HTML Helper辅助函数,这些函数可以生成表单元素并进行表单验证。

  1. 实现用户浏览功能

步骤如下:

  • 在MVC项目的Controller文件夹下创建UserController.cs文件,设置如下代码:
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebApplication.Models;

namespace WebApplication.Controllers
{
    public class UserController : Controller
    {
        private readonly List<User> _users = new List<User>();

        public UserController()
        {
            _users.Add(new User { Id = 1, Name = "User1", Email = "user1@example.com", Password = "123456", ConfirmPassword = "123456" });
            _users.Add(new User { Id = 2, Name = "User2", Email = "user2@example.com", Password = "123456", ConfirmPassword = "123456" });
            _users.Add(new User { Id = 3, Name = "User3", Email = "user3@example.com", Password = "123456", ConfirmPassword = "123456" });
        }

        public ActionResult Index()
        {
            return View(_users);
        }

        public ActionResult Details(int id)
        {
            var user = _users.FirstOrDefault(u => u.Id == id);
            return View(user);
        }
    }
}
  • 在MVC项目的Views文件夹下创建User文件夹,并在该文件夹下创建Index.cshtml文件,设置如下代码:
@model List<WebApplication.Models.User>

@{
    ViewBag.Title = "Index";
}

<h2>@ViewBag.Title</h2>

<p>
    @Html.ActionLink("Create New", "Register")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>
  • 在MVC项目的Views文件夹下创建User文件夹,并在该文件夹下创建Details.cshtml文件,设置如下代码:
@model WebApplication.Models.User

@{
    ViewBag.Title = "Details";
}

<h2>@ViewBag.Title</h2>

<div>
    <h4>User</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Email)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Email)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Password)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Password)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.ConfirmPassword)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.ConfirmPassword)
        </dd>
    </dl>
</div>
<div>
    @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
    @Html.ActionLink("Back to List", "Index")
</div>

示例说明:

在用户浏览与详情页面中,使用了Html.DisplayFor和Html.ActionLink等HTML Helper辅助函数,可方便地生成数据展示和链接等元素。

  1. 使用Bootstrap样式美化界面

在视图文件中使用Bootstrap,使网站界面更加美观。示例代码如下:

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Register" class="btn btn-primary" />
        </div>
    </div>
}

示例说明:

在表单中添加了CSS类“form-control”,使用Bootstrap的表单样式;在提交按钮中添加了CSS类“btn btn-primary”,使用Bootstrap的按钮样式。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:ASP.NET MVC5网站开发之用户添加和浏览2(七) - Python技术站

(0)
上一篇 2023年6月11日
下一篇 2023年6月11日

相关文章

  • js学习笔记之事件处理模型

    JS学习笔记之事件处理模型 简介 在 Web 开发中,事件处理是一个非常重要的部分。事件处理模型就是规定了当事件发生时可以采取哪些行动。JavaScript 作为 Web 开发中最常用的语言,其事件处理模型主要分为三种:内联模型、传统模型和 DOM2 级模型。本篇文章将会详细讲解这三种事件处理模型的原理及其优缺点,以及如何使用它们。 内联模型 内联模型就是将…

    JavaScript 2023年6月10日
    00
  • JS查找孩子节点简单示例

    JS查找孩子节点是在前端开发中常用的操作,可以通过它来查找DOM树中某个节点的直接子节点或者所有子孙节点。以下是JS查找孩子节点的完整攻略: 1. 获取父节点元素 首先需要获取需要查找孩子节点的父元素,可以使用 querySelector 或者 getElementById 等方式获取DOM树中对应的父节点元素。例如: const parentEle = d…

    JavaScript 2023年6月10日
    00
  • 原生js实现日历效果

    原生js实现日历效果 实现日历效果,需要完成以下几个步骤: 获取年月数据 绘制日历框架 填充日期数据 绑定事件 1. 获取年月数据 通过Date()获取当前时间信息,包括年、月、日等信息。 const currentDate = new Date(); let currentYear = currentDate.getFullYear(); let curr…

    JavaScript 2023年5月27日
    00
  • jquery 操作DOM案例代码分享

    下面是详细讲解 “jquery 操作 DOM 案例代码分享” 的完整攻略。 简介 在网页设计和开发中,DOM 操作是重要的一环。jQuery 是一个非常流行的 JavaScript 库,它为 DOM 操作提供了简单、快捷的解决方案,尤其适合移动端开发。在本篇文章中,我们将介绍 jQuery 操作 DOM 的一些简单用法和代码示例。同时,我们会通过示例讲解如何…

    JavaScript 2023年6月10日
    00
  • JavaScript中二维数组的创建技巧

    创建二维数组在JavaScript中非常常见,以下是创建二维数组的几种技巧: 手动创建二维数组 可以使用双重循环来手动创建二维数组,第一层循环用于创建二维数组的行,第二层循环用于创建二维数组的列,如下所示: // 创建一个3*3的二维数组 let arr = []; for (let i = 0; i < 3; i++) { arr[i] = []; …

    JavaScript 2023年5月27日
    00
  • Javascript从数组中随机取出不同元素的两种方法

    下面是Javascript从数组中随机取出不同元素的两种方法的完整攻略。 方法1: 使用splice()方法 splice()方法:用于删除、添加和替换数组中指定的元素,返回值是删除的元素组成的数组。 使用 splice() 方法从数组中随机取出元素时,我们需要使用 Math.random() 生成一个随机的下标值,然后将对应的元素从数组中删除并返回该元素。…

    JavaScript 2023年6月10日
    00
  • JS将指定的某个字符全部转换为其他字符实例代码

    下面是完整的攻略,包含了示例代码和说明: 思路: 我们可以通过JS的字符串处理方法,将指定字符串中的某个字符全部替换为其他字符。具体而言,我们可以使用字符串的replace()函数实现替换功能,该函数接受两个参数,分别表示要替换的字符和用于替换的字符。 下面是基本的replace()函数语法: str.replace(searchValue, replace…

    JavaScript 2023年5月28日
    00
  • javascript StringBuilder类实现

    为了讲解“JavaScript StringBuilder类实现”的完整攻略,我先介绍一下字符串拼接的过程。 在JavaScript中,我们可以使用+运算符或者concat方法来拼接字符串,例如: var str = ‘hello’ + ‘world’; var str1 = ‘hello’.concat(‘ ‘, ‘world’); 但是,当需要将多个字符…

    JavaScript 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部