在entityframwork中调用savechanges()时如何保存记录? [英] How save records when calling savechanges() in entityframwork?

查看:63
本文介绍了在entityframwork中调用savechanges()时如何保存记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在通过指导Asp.net教程创建MVC应用程序MvcMovies(http://www.asp.net/mvc/tutorials/getting-开始使用aspnet-mvc3 / cs / access-your-model'-data-from-a-controller [^])



我创建了sql服务器数据库并创建了一个表格电影,



我唯一的问题是在调用savechanges之后存储数据的位置()



如果你想要代码,



下面是我的代码模型控制器和视图,



Movies.cs



Hello,

I am Creating MVC Application MvcMovies by guiding Asp.net tutorial(http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model''s-data-from-a-controller[^])

I have created sql server database and created a table Movies,

my only question is that where to store data after called savechanges()

if you want code than,

Below is my code model controller and view,

Movies.cs

public class Movies
    {
        public int id { get; set; }
        public string title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal price { get; set; }
        public string Ratings { get; set; }

    }

    public class MoviesDBContext : DbContext
    {
        public DbSet<Movies> Movies { get; set; }
    }





MoviesController.cs





MoviesController.cs

//
       // GET: /Movies/Create

       public ActionResult Create()
       {
           return View();
       }

       //
       // POST: /Movies/Create

       [HttpPost]
       public ActionResult Create(Movies movies)
       {
           if (ModelState.IsValid)
           {
               db.Movies.Add(movies);
               db.SaveChanges();
               return RedirectToAction("Index");
           }

           return View(movies);
       }





create.cshtml



create.cshtml

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Movies</legend>

        <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>

        <div class="editor-label">
            @Html.LabelFor(model => model.Ratings)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Ratings)
            @Html.ValidationMessageFor(model => model.Ratings)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}





我需要知道,当我创建新记录时,它将会是插入。



我检查了数据库表但没有记录。



谢谢。



I need to know that when I am creating new record where it will be inserted.

I have checked database table but there is no records.

Thanks.

推荐答案

你试图保存它而不传递任何价值。

只需修改你发布的方法为



You are trying to save it without passing any value.
just modify you post method as

[HttpPost]
        public ActionResult Create(Movies movies)
        {
            if (ModelState.IsValid)
            {
                movies.title=title,
                movies.Genre=Genre,
                movies.ReleaseDate=ReleaseDate,
                movies.price=price,
                movies.Ratings=Ratings
                db.Movies.Add(movies);
                db.SaveChanges();
                return RedirectToAction(&amp;amp;quot;Index&amp;amp;quot;);
            }

            return View(movies);
        }

Please feel free to comment	;-)


这篇关于在entityframwork中调用savechanges()时如何保存记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆