如何使用mvc3在数据库中插入数据 [英] how to insert data in database using mvc3
本文介绍了如何使用mvc3在数据库中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何使用mvc3在数据库中插入数据?
解决方案
参考:
使用MVC的ASP.NET应用程序开发 [ ^ ]
使用jQuery for Beginners的ASP.NET MVC3 Razor [ ^ ]
ASP。 NET MVC 3:创建一个简单的注册表单 [ ^ ]
..等等 google [ ^ ]
hi
关注此
http://www.c-sharpcorner.com/UploadFile/e5b214/Asp-Net-mvc-3-database-connectivity-using-entity-framework/
步骤1:为您的表添加ADO.NET实体数据模型,它将添加到模型文件夹中
步骤2:为您所在的表创建一个类要在模型文件夹中插入数据
public class movie
{
public string title { get ; set ; }
public DateTime releaseate { get ; set ; }
public string genre { get 跨度>; set ; }
public decimal price { get 跨度>; set ; }
}
步骤3:创建一个控制器,在其中放置插入方法的代码
步骤4:创建控制器文件中的两个动作,一个用于HttpGet和HttpPost
[HttpGet]
public ActionResult addmovie()
{
return View();
}
[HttpPost]
public ActionResult addmovie(movie mv)
{
Movie newmv = < span class =code-keyword> new Movie();
newmv.Title = mv.title;
newmv.ReleaseDate = mv.releasedate;
newmv.Genre = mv.genre;
newmv.Price = Convert.ToDecimal(mv.price);
db.Movies.AddObject(newmv);
db.SaveChanges();
return View();
}
步骤5:为我们刚创建的动作创建视图
@ model MvcMovie .Models.movie
@ {
ViewBag.Title =addmovie;
}
< h2 > addmovie < / h2 >
@using(Html.BeginForm())
{
< ; label > 标题:< / label >
@ Html.TextBoxFor(m => m .title)
@ Html.ValidationMessageFor(m => m.title)
< label > 发布DAte:< / label >
@ Html.TextBo xFor(m => m.releasedate)
@ Html.ValidationMessageFor(m => m.releasedate)
< ; 标签 > 类型:< / label >
@ Html.TextBoxFor(m => m .genre)
@ Html.ValidationMessageFor(m => m.genre)
< 标签 > 价格:< / label >
@ Html.TextBoxFor(m => m.price)
@ Html.ValidationMessageFor(m => m.price)
< 输入 id = Submit1 type = 提交 value = 添加电影 / >
}
how we can insert data in database using mvc3?
解决方案
Refer:
ASP.NET Application Development Using MVC[^]
ASP.NET MVC3 Razor With jQuery For Beginners[^]
ASP.NET MVC 3: CREATING A SIMPLE SIGN-UP FORM[^]
..and more on google[^]
hi
follow this
http://www.c-sharpcorner.com/UploadFile/e5b214/Asp-Net-mvc-3-database-connectivity-using-entity-framework/
Step 1 : Add ADO.NET entity data model for your table, it will be added in model folder
Step 2 : Create a class for the table in which you are going to insert data in model folder
public class movie { public string title { get; set; } public DateTime releasedate { get; set; } public string genre { get; set; } public decimal price { get; set; } }
Step 3 : Create a controller in which you are going to put code for insert method
Step 4 : Create two actions in controller file, one for HttpGet and HttpPost
[HttpGet] public ActionResult addmovie() { return View(); } [HttpPost] public ActionResult addmovie(movie mv) { Movie newmv = new Movie(); newmv.Title = mv.title; newmv.ReleaseDate = mv.releasedate; newmv.Genre = mv.genre; newmv.Price = Convert.ToDecimal(mv.price); db.Movies.AddObject(newmv); db.SaveChanges(); return View(); }
Step 5 : Create the view for actions we just created
@model MvcMovie.Models.movie @{ ViewBag.Title = "addmovie"; } <h2>addmovie</h2> @using (Html.BeginForm()) { <label>Title : </label> @Html.TextBoxFor(m=>m.title) @Html.ValidationMessageFor(m=>m.title) <label>Release DAte : </label> @Html.TextBoxFor(m=>m.releasedate) @Html.ValidationMessageFor(m=>m.releasedate) <label>Genre : </label> @Html.TextBoxFor(m=>m.genre) @Html.ValidationMessageFor(m=>m.genre) <label>Price : </label> @Html.TextBoxFor(m=>m.price) @Html.ValidationMessageFor(m=>m.price) <input id="Submit1" type="submit" value="Add Movie" /> }
这篇关于如何使用mvc3在数据库中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文