使用c#

时间:2017-12-26 06:27:36

标签: javascript c# ajax mongodb

这是我的c#代码

public static void updateSubmit(string id,string fname,string lname,string email,string password,string address)
{
    string connectionString = "mongodb://10.10.32.125:27017";
       MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
       MongoClient mongoClient = new MongoClient(settings);
       var Server = mongoClient.GetDatabase("mongovaibhav");
       var collection = Server.GetCollection<employee>("mongov");
       ObjectId objectId = ObjectId.Parse(id);
       var filter = Builders<employee>.Filter.Eq(s => s._id, objectId);
       employee emp = new employee();
       emp.fname = fname;
       emp.lname = lname;
       emp.email = email;
       emp.pass = password;
       emp.address = address;
       collection.ReplaceOneAsync(filter, emp);
}

这是我发送更新请求和数据的ajax代码

 function updateSubmit()
    {
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'Home.aspx/updateSubmit',
            data: "{'id':'" + $("#hidden").val()+ "','fname':'" + $("#fname").val() + "','lname':'" + $("#lname").val() + "','email':'" + $("#email").val() + "','password':'" + $("#password").val() + "','address':'" + $("address").val() + "'}",
            async: false,
            success: function (response) {
                alert("You Have SuccessFully Update Data");
            },
            error: function () {
                console.log('there is some error');
            }
        });
    }

现在我的问题是,我收到警告消息,表示您已成功更新记录,但记录无法更改数据库中的效果

1 个答案:

答案 0 :(得分:0)

我得到的解决方案我在我的“Param”变量中有错误我在哪里写密码Insted of“pass”因为我的员工类包含“pass”属性 谢谢大家@souvik @felix

  string connectionString = "mongodb://10.10.32.125:27017";
            MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
            MongoClient mongoClient = new MongoClient(settings);
            var Server = mongoClient.GetDatabase("mongovaibhav");
            var collection = Server.GetCollection<employee>("mongov");
            ObjectId objectId = ObjectId.Parse(id);
              var filter = Builders<employee>.Filter.Eq(s => s._id, objectId);   
            string param = "{$set: { fname:'" + fname + "',lname:'" + lname + "',email:'" + email + "',pass:'" + password + "',address :'" + address + "' } }";
            BsonDocument document = BsonDocument.Parse(param);
            collection.UpdateMany(filter, document);