日期:2014-05-17  浏览次数:20454 次

请问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");