ASP.Net MVC -『Insert Data to DB』

//[Model]『CoderViewModel.cs』 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace OA.ViewModels { public class CoderViewModel { public decimal id { get; set; } public string Account { get; set; } public string Password { get; set; } public string EMail { get; set; } public string Skill { get; set; } } } //『CoderDBServices.cs』 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity.Validation; using OA.Models; using OA.ViewModels namespace OA.Services { public class CoderDBServices { CoderEntities db = new CoderEntities(); List<Models.Coder> result = new List<Models.Coder>(); #region Insert Coder public void AddCoder(CoderViewModel formModel) { Coder obj= new Coder(); obj.Account = formModel.Account; obj.Password = formModel.Password; obj.EMail = formModel.EMail; obj.Skill = formModel.Skill; db.Coder.Add(obj); try { db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } #endregion } } //[Controller]『CoderController.cs』 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using OA.Services; using OA.ViewModels; using OA.Models; namespace OA.Controllers { public class CoderController : Controller { CoderDBServices coderService = new CoderDBServices(); CoderViewModel formModel = new CoderViewModel(); public ActionResult registration() { return View(); } [HttpPost] public ActionResult registration(string Account, string Password, string EMail, string Skill) { formModel.Account = Account; formModel.Password = Password; formModel.EMail = EMail; formModel.Skill = Skill; coderService.AddCoder(formModel); return View(); } } } //[View]『registration.cshtml』 <body> <form name="form1" action=@Url.Action("registration", "Coder") method="post"> <input type="text" name="Account" value="" /> <input type="password" name="Password" value="" /> <input type="email" name="EMail" value="" /> <input type="text" name="Skill" value="" /> <input type="submit" name="Name" value="register" /> </form> </body>
ASP.Net MVC -『Insert Data to DB』

Hey Guys,
I don't use ASP.Net MVC Razor grammar - HTML Helper to post data,
so I try to another way, just use html form to post data to db,
and I did it ~

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.