老规矩,先上图
接着就是上代码了哦
asp net core MVC,总共就三个文件的代码CSS暂时不上。
1 ZhuCeController.cs 这个是处理逻辑(MVC中的C,controller),是网页加载的时候,首先处理的事件(当然 model会在controller中引用),然后才是其他两个。这个是我个人的理解,新手,有可能理解有点不对,但总归有想法比没有想法的好把,当你发现我理解有误的时候,说明一个问题,你有想法,且你超过了我,恭喜哈
using Microsoft.AspNetCore.Mvc;
using mytest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace mytest.Controllers
{
public class ZhuCeController : Controller
{
// GET: //
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(ZhuCe zhuce)
{
string ch11;
ch11=“您输入:”+zhuce.yonghuming+";"+ zhuce.mima + “;” + zhuce.shoujihao + “;” + zhuce.youxiang + “;”;
zhuce.output = ch11;
return View(zhuce);
//return View(Content(“”));
}
}
}
2 ZhuCe.cs 这个是构建模型哦
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace mytest.Models
{
public class ZhuCe
{
public string yonghuming { get; set; }
public string mima { get; set; }
public string shoujihao { get; set; }
public string youxiang { get; set; }
public string output { get; set; }
}
}
3 Index.cshtml 这个是页面布局
@model ZhuCe
@{
ViewData[“Title”] = “新用户注册”;
Layout = null;
}
<!--引用 用户登录 外联样式表-->
<link href="~/css/yonghudenglu.css" rel="stylesheet" type="text/css" />
<section class="login-form-wrap" style="height:500px">
<h1>新用户注册</h1>
@using (Html.BeginForm("Index", "ZhuCe", FormMethod.Post))
{
<div class="login-form" >
<input type="text" name="yonghuming" value="@Html.DisplayFor(c=>c.yonghuming)" required placeholder="请输入登录用户名" )>
<input type="password" name="mima" value="@Html.DisplayFor(c=>c.mima)" required placeholder="请输入登录密码">
<input type="number" name="shoujihao" value="@Html.DisplayFor(c=>c.shoujihao)" required placeholder="请输入手机号">
<input type="email" name="youxiang" value="@Html.DisplayFor(c=>c.youxiang)" required placeholder="请输入邮箱">
<input name="output" value="@Html.DisplayFor(c=>c.output)" style="margin-top:30px" placeholder="这个是结果" />
<input type="submit" value="注册">
</div>
}
<a href="DengLu" style="color:black;margin-right:30px;">登录</a>
</section>
<div style="text-align:center;">
</div>
备注下:
页面布局的时候,设计到表单提交的问题,都用到using(@Html.BeginForm(“Index”,“ZhuCe”,“Post”))
说下这个里面的三个参数的含义:
1 Index:表示我点击提交按钮(注册)的时候,执行的函数的名字:Index函数;
2 ZhuCe:执行的是那个控制器的函数呢,这里就是指定的ZhuCe里面的哦,其实全路径是:Controllers/ZhuCeController.cs 这个里面就是net core MVC内置的一些规则在里面。对于像我这种刚入门几天的人来说,讲下我的理解。
规则 1: 一个在前台页面 都会对应MVC,其中M(model)可以不需要,其他两个 View和controller是必须要的;
规则2:命名的规则,这个是内置的,如果我们打算的页面名为:xxyy,那么好了,你就得新建得文件名包括:MVC:Models/xxyy.cs;Views/xxyy/xxyy.cshtml;Contrlllers/xxyyControllers.cs