using System; using System.ComponentModel.DataAnnotations; namespace mvc_project1.Models { public class UserDetail { [Required] [RegularExpression(^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")] public string FirstName { get; set;} [Required] [RegularExpression(^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid Last Name")] public string LastName { get; set; } public Guid UserId { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Data; using System.Data.SqlClient; using mvc_project1.Models; using Action = mvc_project1.Models.UserDetail; namespace mvc_project1.Controllers public ActionResult CreateNew(UserDetail objNewUser) { if (ModelState.IsValid) { if (Session["UserList"] == null) { Session["UserList"] = GetUserList(); } var userList = (List<UserDetail>)Session["UserList"]; objNewUser.UserId = Guid.NewGuid(); userList.Add(objNewUser); Session["UserList"] = userList; return RedirectToAction("Index"); } return View(objNewUser); }
@model mvc_project1.Models.UserDetail @{ ViewBag.Title = "CreateNew"; } @using (Html.BeginForm()) { <table> <tr> <td> First Name </td> <td> Last Name </td> </tr> <tbody> <tr> <td> @Html.TextBoxFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName) </td> <td> @Html.TextBoxFor(modal=>modal.LastName) @Html.ValidationMessageFor(model => model.LastName) </td> <td> <input type="submit" value="Add User" /> </td> </tr> </tbody> </table> }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)