如何从表单更新数据库中的值 [英] How to update values in the database from the form

查看:113
本文介绍了如何从表单更新数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的视图文件,当我单击更新时必须在数据库中对其进行更新

This is my view file when i click update it has to be updated in database

myForm.attachEvent("onButtonClick", function (name, command) {
            if (name == "Update" && command == "Update") {     
                            $.ajax({
                    url: '/Home/Update',

                    data: JSON.stringify({
                        Id:myForm.getItemValue("Id"),
                        Name: myForm.getItemValue("Name"),
                        Duration: myForm.getItemValue("Duration"),
                        Year: myForm.getItemValue("Year"),
                        Comments: myForm.getItemValue("Comments")

                    }),
                    
                    cache: false,
                    dataType: "json",
                    success: function (str) {
                        alert(str.st);                    

                    },
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8'
                });
            }
        });


在控制器中,我定义了用于更新的功能


In controller i define function for update

public JsonResult Update(int Id,string Name,int Duration,int Year,string Comments)
        {

            Training d = new Training();

            if (d.Id == Id)
            {
               d.Id=Id;
                d.Name = Name;
                d.Duration = Duration;
                d.Year = Year;
                d.Comments = Comments;
            }
                try
                {
                    this.dbContext.Add(d);
                    this.dbContext.SaveChanges();
                }
                catch
                {

                }

                var str = new { st = "successfully updated" };

                return Json(str, JsonRequestBehavior.AllowGet);
        }

推荐答案

.ajax({ 网址:' /Home/Update', 数据: JSON .stringify({ Id:myForm.getItemValue(" ), 名称:myForm.getItemValue(" ), 持续时间:myForm.getItemValue(" ), 年:myForm.getItemValue(" ), 注释:myForm.getItemValue(" ) }), 缓存: false , dataType:" , 成功:功能(str){ 警报(str.st); }, 类型:' POST', contentType:' application/json; charset = utf-8' }); } });
.ajax({ url: '/Home/Update', data: JSON.stringify({ Id:myForm.getItemValue("Id"), Name: myForm.getItemValue("Name"), Duration: myForm.getItemValue("Duration"), Year: myForm.getItemValue("Year"), Comments: myForm.getItemValue("Comments") }), cache: false, dataType: "json", success: function (str) { alert(str.st); }, type: 'POST', contentType: 'application/json; charset=utf-8' }); } });


在控制器中,我定义了用于更新的功能


In controller i define function for update

public JsonResult Update(int Id,string Name,int Duration,int Year,string Comments)
        {

            Training d = new Training();

            if (d.Id == Id)
            {
               d.Id=Id;
                d.Name = Name;
                d.Duration = Duration;
                d.Year = Year;
                d.Comments = Comments;
            }
                try
                {
                    this.dbContext.Add(d);
                    this.dbContext.SaveChanges();
                }
                catch
                {

                }

                var str = new { st = "successfully updated" };

                return Json(str, JsonRequestBehavior.AllowGet);
        }


查看这些链接,您将有所了解

http://forums.asp.net/t/1655940.aspx/1 [ ^ ]

See these Links You will get some idea

http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios.aspx[^]

http://forums.asp.net/t/1655940.aspx/1[^]

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application[^]


这篇关于如何从表单更新数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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