using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcModelApp { [MetadataType(typeof(UserMetadata))] public partial class tb_User { public string RePwd { get; set; } } public class UserMetadata { [DisplayName("用户名")] [Remote("NotExitesUserName", "Home")] public string UserName { get; set; } [DisplayName("备注")] [DataType(DataType.MultilineText)] public string Remark { get; set; } [DisplayName("年龄")] [Range(1, 120)] public int Age { set; get; } [DisplayName("密码")] [PasswordPropertyText] public string Pwd { get; set; } [PasswordPropertyText] [DisplayName("重述密码")] [System.Web.Mvc.Compare("Pwd")] public string RePwd { get; set; } [Email] public string Email { get; set; } } public class EmailAttribute : RegularExpressionAttribute { public EmailAttribute() : base(@"^[a-z0-9]+([._\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$") { } } }
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ 这个是ADO.NET EF 自动生成的类 namespace MvcModelApp { using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; public partial class tb_User { public int ID { get; set; } public string UserName { get; set; } public string Remark { get; set; } public int Age { get; set; } public string Pwd { get; set; } public string Email { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcModelApp.Controllers { public class HomeController : Controller { // // GET: /Home/ private TestMvcEntities db = new TestMvcEntities(); public ActionResult Index() { var list = db.tb_User.ToList(); return View(list); } public ActionResult Register() { tb_User model = new tb_User(); return View(model); } [HttpPost] public ActionResult Register(tb_User model) { if (ModelState.IsValid) { db.tb_User.Add(model); db.SaveChanges(); return RedirectToAction("Index"); } else { return View(); } } [HttpGet] public JsonResult NotExitesUserName() { string UserName = Request.Params["UserName"]; var user = db.tb_User.Where(c => c.UserName == UserName).FirstOrDefault(); return user == null ? Json(true, JsonRequestBehavior.AllowGet) : Json(false, JsonRequestBehavior.AllowGet); } public JsonResult TestArr(List<string> arr) { //string ss= return Json(new {success=1 }); } } }
@model IEnumerable<MvcModelApp.tb_User> @{ ViewBag.Title = "Index"; } <script src="~/Scripts/jquery-1.7.2.min.js"></script> <script src="~/Scripts/jquery.validate.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.js"></script> <h2>Index</h2> <script type="text/javascript"> $(document).ready(function () { //$(":text").map(function () { // alert($(this).val()); //}) var arr = new Array(); //var objstr = $(":text").map(function () { // return $(this).text(); //}).get().join(","); $("#btnTest").click(function () { // $(":text").each(function () { alert($(this).val());}); $("input[id^='txt']").each(function () { arr.push($(this).val()); // alert($(this).val()); }); // var jsonobj = {"arr":arr,"ljj":"1"}; $.ajax({ type: "POST", url: "@Url.Action("TestArr", "Home")", data: JSON.stringify(arr), contentType: "application/json", success: function (data, status) { alert(data); } }); // var str = arr.join(","); // alert(str); }); }); // var obj = </script> <p> <input type="text" id="txt1" /> <input type="text" id="txt2" /> <input type="text" id="txt3" /> <input type="text" id="txt4" /> <input id="btnTest" type="button" value="测试传递数组对象" /> </p> <p> @Html.ActionLink("Register","Register", "Home") </p> <table> <tr> <th> @Html.DisplayNameFor(model => model.RePwd) </th> <th> @Html.DisplayNameFor(model => model.UserName) </th> <th> @Html.DisplayNameFor(model => model.Remark) </th> <th> @Html.DisplayNameFor(model => model.Age) </th> <th> @Html.DisplayNameFor(model => model.Pwd) </th> <th> @Html.DisplayNameFor(model => model.Email) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.RePwd) </td> <td> @Html.DisplayFor(modelItem => item.UserName) </td> <td> @Html.DisplayFor(modelItem => item.Remark) </td> <td> @Html.DisplayFor(modelItem => item.Age) </td> <td> @Html.DisplayFor(modelItem => item.Pwd) </td> <td> @Html.DisplayFor(modelItem => item.Email) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) </td> </tr> } </table>
@model MvcModelApp.tb_User @{ ViewBag.Title = "Register"; } <script src="~/Scripts/jquery-1.7.2.min.js"></script> <script src="~/Scripts/jquery.validate.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.js"></script> <h2>Register</h2> @using (Html.BeginForm("Register","Home",null,FormMethod.Post,new {id="form1"})) { <table> <tr> <td>@Html.Display("UserName")</td> <td>@Html.TextBoxFor(x=>x.UserName)@Html.ValidationMessageFor(x=>x.UserName)</td> </tr> <tr> <td>@Html.DisplayFor(x=>x.Age)</td> <td>@Html.TextBoxFor(x=>x.Age)@Html.ValidationMessageFor(x=>x.Age)</td> </tr> <tr> <td>@Html.DisplayFor(x=>x.Pwd)</td> <td>@Html.TextBoxFor(x=>x.Pwd)@Html.ValidationMessageFor(x=>x.Pwd)</td> </tr> <tr> <td>@Html.DisplayFor(x=>x.RePwd)</td> <td>@Html.TextBoxFor(x=>x.RePwd)@Html.ValidationMessageFor(x=>x.RePwd)</td> </tr> <tr> <td>@Html.DisplayFor(x=>x.Email)</td> <td>@Html.TextBoxFor(x=>x.Email)@Html.ValidationMessageFor(x=>x.Email)</td> </tr> <tr> <td>@Html.DisplayFor(x=>x.Remark)</td> <td>@Html.TextBoxFor(x=>x.Remark)@Html.ValidationMessageFor(x=>x.Remark)</td> </tr> <tr> <td colspan="2"><input type="submit" value="注册"/></td> </tr> </table> }