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>

}
相关推荐
睡觉谁叫~~~35 分钟前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
2401_865854883 小时前
iOS应用想要下载到手机上只能苹果签名吗?
后端·ios·iphone
AskHarries3 小时前
Spring Boot集成Access DB实现数据导入和解析
java·spring boot·后端
2401_857622663 小时前
SpringBoot健身房管理:敏捷与自动化
spring boot·后端·自动化
程序员阿龙3 小时前
基于SpringBoot的医疗陪护系统设计与实现(源码+定制+开发)
java·spring boot·后端·医疗陪护管理平台·患者护理服务平台·医疗信息管理系统·患者陪护服务平台
程思扬4 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
阿华的代码王国4 小时前
【Spring】——SpringBoot项目创建
java·spring boot·后端·启动类·target文件
九鼎科技-Leo4 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
阿芯爱编程4 小时前
平衡二叉树
java·后端·算法
程序员清风5 小时前
浅析Web实时通信技术!
java·后端·面试