asp.net core mvc

Controllers
WrHelloWorldController.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Controllers
{
    public class WrHelloWorldController : Controller
    {
        // GET: WrHelloWorldController
        public ActionResult Index()
        {
            return View();
        }

        // GET: WrHelloWorldController/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: WrHelloWorldController/Create
        public ActionResult Create()
        {
            var plst = new List<object>();
            foreach (var x in Enum.GetValues(typeof(Models.Gender)))
            {
                plst.Add(new
                {
                    ID = Convert.ToInt32(x),
                    Name = x.ToString()
                });
            }
            SelectList sellist1 = new SelectList(plst, "ID", "Name","3");
            ViewData["GenderList"] =sellist1;
            return View();
        }

        // POST: WrHelloWorldController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: WrHelloWorldController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: WrHelloWorldController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken] //防止跨网站请求伪造令牌
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: WrHelloWorldController/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: WrHelloWorldController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken] //防止跨网站请求伪造令牌
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}

Models

StudentViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class StudentViewModel
    {
       
        public string Name { get; set; }
        public string Age { get; set; }
        public string Height { get; set; }
       
        [EnumDataType(typeof(Gender))]
        public Gender Gender { get; set; }
    }
    public enum Gender
    {
        一年级=1,
        二年级=2,
        三年级=3,
        四年级=4,
        五年级=5,
        六年级=6
    }
}

Views

Create.cshtml
 

@model WebApplication1.Models.StudentViewModel

@using (Html.BeginForm("Create", "WrHelloWorld", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @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.TextBoxFor(model => model.Name, null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("3",ViewData["GenderList"] as SelectList)
            @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="提交" class="btn btn-default" />
        </div>
    </div>

}



相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 07:20:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 07:20:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 07:20:07       87 阅读
  4. Python语言-面向对象

    2024-06-08 07:20:07       96 阅读

热门阅读

  1. Spring Boot集成geodesy实现距离计算

    2024-06-08 07:20:07       22 阅读
  2. SQL查询的优化方案

    2024-06-08 07:20:07       37 阅读
  3. npm常用命令,包含移除,安装等

    2024-06-08 07:20:07       31 阅读
  4. Zookeeper 和 Eureka 的区别

    2024-06-08 07:20:07       25 阅读
  5. Apache Kylin:大数据分析从入门到精通

    2024-06-08 07:20:07       32 阅读
  6. UE5-AI

    UE5-AI

    2024-06-08 07:20:07      30 阅读
  7. vb.net学习总结

    2024-06-08 07:20:07       26 阅读
  8. Android 14.0 修改wifi信号强度和信号等级

    2024-06-08 07:20:07       26 阅读
  9. 深度学习中2D检测

    2024-06-08 07:20:07       28 阅读
  10. kafka消息积压处理方案

    2024-06-08 07:20:07       28 阅读
  11. opencv灰度变换

    2024-06-08 07:20:07       29 阅读
  12. Apache Spark MLlib详解

    2024-06-08 07:20:07       29 阅读
  13. cpp--lua--cpp执行lua

    2024-06-08 07:20:07       27 阅读