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>

}
相关推荐
2401_854391089 分钟前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
虽千万人 吾往矣30 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
这孩子叫逆1 小时前
Spring Boot项目的创建与使用
java·spring boot·后端
coderWangbuer2 小时前
基于springboot的高校招生系统(含源码+sql+视频导入教程+文档+PPT)
spring boot·后端·sql
攸攸太上2 小时前
JMeter学习
java·后端·学习·jmeter·微服务
Kenny.志2 小时前
2、Spring Boot 3.x 集成 Feign
java·spring boot·后端
sky丶Mamba3 小时前
Spring Boot中获取application.yml中属性的几种方式
java·spring boot·后端
千里码aicood4 小时前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
程序员-珍4 小时前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
liuxin334455664 小时前
教育技术革新:SpringBoot在线教育系统开发
数据库·spring boot·后端