在MVC4中更新数据库记录

时间:2015-08-21 05:39:42

标签: entity-framework asp.net-mvc-4 model-view-controller

需要一些帮助..

目标:更新MVC4中数据库的记录

我有一个带有两个按钮的Accordian视图#1.Edit#2.save .. 直到现在所做的是......

使用以下代码我将数据从数据库中提取到相应的文本框。

步骤1:一旦编辑按钮单击的文本框设置为可编辑,文本框中填充的数据属性设置为只读。

步骤2:如果我更改数据并单击应在数据库上更新的保存按钮

问题:

更新数据库中的数据时,更新的数据会在新行中添加新数据。 但我需要更新现有数据

查看:

<body>

    <div id='cssmenu'>
        <ul>
            <li class='active'></li>
            <li class='has-sub'>
                <a href='#'><span>Profile</span></a>
                <ul>
                    <form method="post" >
                    <table>
                        <tr>
                            <td>
                                @Html.DisplayNameFor(model => model.FirstName)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.FirstName, new { @readonly = "readonly" })
                            @*@Html.EditorFor(model => model.FirstName)*@
                                @Html.ValidationMessageFor(model => model.FirstName)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.DisplayNameFor(model => model.LastName)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.LastName, new { @readonly = "readonly" })
                                @Html.ValidationMessageFor(model => model.LastName)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.DisplayNameFor(model => model.EmailID)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.EmailID, new { @readonly = "readonly" })
                                @Html.ValidationMessageFor(model => model.EmailID)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.DisplayNameFor(model => model.MobileNumber)
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.MobileNumber, new { @readonly = "readonly" })
                                @Html.ValidationMessageFor(model => model.MobileNumber)
                            </td>
                        </tr>
                    </table>
                 <input type="button" id="btn" name="Edit" value="Edit"/>
                        <input type="submit" name="Save" id="Save" />
                        @*<input type="submit" name="Save" value="Save" onclick="location.href='@Url.Action("Save", "CU")'" />*@
               </form>
                </ul>
            </li>


             <li class='has-sub'>
                <a href='#'><span>Change Password</span></a>
                <ul>
                    <li><a href='#'><span>Company</span></a></li>
                    <li class='last'><a href='#'><span>Contact</span></a></li>
                </ul>
            </li>
            <li class='has-sub'>
                <a href='#'><span>Add Customer</span></a>
                <ul>
                    <li><a href='#'><span>Company</span></a></li>
                    <li class='last'><a href='#'><span>Contact</span></a></li>
                </ul>
            </li>
        </ul>
    </div>

</body>

@section scripts
{

    <script type="text/javascript">

        $("#btn").click(function () {
            $("#FirstName").removeAttr("readonly");
            $("#LastName").removeAttr("readonly");
            $("#EmailID").removeAttr("readonly");
            $("#MobileNumber").removeAttr("readonly");

        });

    </script>

    }

控制器

 public ActionResult AccountPanel(int id, string Save, string FirstName)
        {

            var profile = (from s in db.tblUsers
                               where s.UserTypeId == 3 && s.UserID == id
                               select new Profile
                               {
                                   FirstName = s.FirstName,
                                   LastName = s.LastName,
                                   EmailID = s.EmailID,
                                   MobileNumber = s.MobileNumber


                               }).FirstOrDefault();



                if (Save != null)
                {

                    using (var context = new SYTEntities())
                    {
                        var s = context.tblUsers.Where(a => a.UserID == id).FirstOrDefault();
                            s.FirstName = FirstName;
                            s.LastName = profile.LastName;
                            s.EmailID = profile.EmailID;
                            s.MobileNumber = profile.MobileNumber;

                            context.tblUsers.Add(s);

                            context.SaveChanges();


                    }


                }



                return View(profile);
        }

2 个答案:

答案 0 :(得分:1)

要更新数据库中的数据,请转到下面的代码。

if (Save != null)
{

    using (var context = new SYTEntities())
    {
        var s = context.tblUsers.Where(a => a.UserID == id).FirstOrDefault();
        s.FirstName = FirstName;
        s.LastName = profile.LastName;
        s.EmailID = profile.EmailID;
        s.MobileNumber = profile.MobileNumber;

        context.SaveChanges();

    }

}

从代码中删除context.tblUsers.Add(s);

答案 1 :(得分:1)

using (var context = new SYTEntities())
{
    var s = context.tblUsers.Where(a => a.UserID ==     id).FirstOrDefault();
    s.FirstName = FirstName;
    s.LastName = profile.LastName;
    s.EmailID = profile.EmailID;
    s.MobileNumber = profile.MobileNumber;

    // SET STATE TO CHANGED
    db.Entry(tblUsers).State = EntityState.Modified;

    context.SaveChanges();

}