当前位置: 代码迷 >> ASP.NET >> 请教MVC3.0中的保存有关问题
  详细解决方案

请教MVC3.0中的保存有关问题

热度:3457   发布时间:2013-02-25 00:00:00.0
请问MVC3.0中的保存问题!
最近刚开始学习MVC3.0
自己添加了个Controllers
 
C# code
 public class MoviesController : Controller    {        //        // GET: /Movies/        MovieDBContext db = new MovieDBContext();        public ActionResult Index()        {            var movies = from m in db.Movies                         where m.ReleaseDate > new DateTime(1984, 6, 1)                         select m;          //  return View();            return View(movies.ToList());        }        public ActionResult CreateDocument()        {            return View();        }        [HttpPost]        public ActionResult CreateDocument(Movie newMovie)        {            if (ModelState.IsValid)            {                db.Movies.Add(newMovie);                db.SaveChanges();                return RedirectToAction("Index");            }            else                return View(newMovie);        }    }

运行起来 怎么就不能保存呢?
C# code
@model MvcApplicationLKOne.Models.Movie@{    ViewBag.Title = "CreateDocument&新建一条数据";}<h2>CreateDocument</h2><script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>@using (Html.BeginForm()) {    @Html.ValidationSummary(true)    <fieldset>        <legend>Movie</legend>           @Html.HiddenFor(Model=>Model.ID)        <div class="editor-label">            @Html.LabelFor(model => model.Title)        </div>        <div class="editor-field">            @Html.EditorFor(model => model.Title)            @Html.ValidationMessageFor(model => model.Title)        </div>        <div class="editor-label">            @Html.LabelFor(model => model.ReleaseDate)        </div>        <div class="editor-field">            @Html.EditorFor(model => model.ReleaseDate)            @Html.ValidationMessageFor(model => model.ReleaseDate)        </div>        <div class="editor-label">            @Html.LabelFor(model => model.Genre)        </div>        <div class="editor-field">            @Html.EditorFor(model => model.Genre)            @Html.ValidationMessageFor(model => model.Genre)        </div>        <div class="editor-label">            @Html.LabelFor(model => model.Price)        </div>        <div class="editor-field">            @Html.EditorFor(model => model.Price)            @Html.ValidationMessageFor(model => model.Price)        </div>        <p>            <input type="submit" value="Create" />        </p>    </fieldset>}<div>    @Html.ActionLink("Back to List", "Index")</div>


------解决方案--------------------------------------------------------
在 if (ModelState.IsValid) 下一个断点。
------解决方案--------------------------------------------------------
View:
Html.BeginForm()
=>
Html.BeginForm("CreateDocument", "Movies")

Controller:

public ActionResult CreateDocument(Movie newMovie)
{
if (ModelState.IsValid)
{
UpdateModel(newMovie);
db.Movies.Add(newMovie);
db.SaveChanges();
return RedirectToAction("Index");
}
else
  相关解决方案