更新我的数据库 - 失败

时间:2018-06-18 09:00:46

标签: asp.net-mvc mongodb

更新:我多次调试它,似乎' UpdateUserDetails'函数很好,但我认为问题出在这一行(在UpdateUser函数中):

var filter = Builders<User>.Filter.Eq("ID", input.ID);

我有一个网站(我还没有在线上传),我在本地主机上使用MongoDB。我有USER SYSTEM(包括登录,注册......)。我试图添加一个选项来管理管理员手动编辑任何注册用户,但我一直没有使用新的&#39;版本来更新数据库。用户。

附上相关代码..

〜更新功能:

[HttpPost]
    public ActionResult UpdateUser(User input)
    {
        // var a = input.ID;
        var filter = Builders<User>.Filter.Eq("ID", input.ID);
        var update = Builders<User>.Update
        .Set("FirstName", input.FirstName)
        .Set("LastName", input.LastName)
        .Set("Phone", input.Phone)
        .Set("Email", input.Email)
        .Set("Password", input.Password)
        .Set("Permission", input.Permission);
        DBManager.UpdateUserDetails(filter, update);
        ViewBag.Update = "Update successful";
        return RedirectToAction("UsersList");
    }

〜我的DBManager类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Linq;
using SurffingSite.Models;



namespace SurffingSite.DB
{
    public class DBManager
    {
        static MongoClient client;
        static IMongoDatabase DB;
        // Constractor
        static DBManager()
        {
            client = new MongoClient("mongodb://localhost:27017");
            DB = client.GetDatabase("HELLOSTACKOVERFLOW");
        }
        // Method to get list of all users//
        public static IMongoCollection<User> GetUsersCollection()
        {
            var collection = DB.GetCollection<User>("User");
            return collection;
        }

        internal static object GetCollection<T>(string v)
        {
            throw new NotImplementedException();
        }

                                           //User Method  //

        // Method for adding new user
        public static void AddNewUser(User user)
        {
            var collection = DB.GetCollection<User>("User");
            collection.InsertOne(user);
        }
        //Get user email.
        public static User GetUserEmail(User input)
        {
            User email = DBManager.GetUsersCollection().Find(user => user.Email == input.Email).FirstOrDefault();
            return email;
        }

        //Get user passsword.
        public static User GetUserPassword(User input)
        {
            User password = DBManager.GetUsersCollection().Find(user => user.Password == input.Password).FirstOrDefault();
            return password;
        }
        //User login verification 
        public static User UserVerification(User input)
        {
            // try get user by email
            // if user exist than check if the password is correct if not than return null
            User UseVar = DBManager.GetUserEmail(input);
            if (UseVar != null && UseVar.Password == input.Password)
            {
                return UseVar;
            }
            return null;
        }
        //Update User details
        public static void UpdateUserDetails(FilterDefinition<User> filter, UpdateDefinition<User> update)
        {
            var collection = DB.GetCollection<User>("User");
            collection.UpdateOne(filter, update);
        }

        //Products Method's//

        // Adding product into DB
        public static void AddProduct(Products product)
        {
            var collection = DB.GetCollection<Products>("Products");
            collection.InsertOne(product);
        }
        public static IMongoCollection<Products> GetProductsCollection()
        {
            var collection = DB.GetCollection<Products>("Products");
            return collection;
        }
        // Find product in DB by ID
        public static Products FindProductsById(Products input)
        {
            Products searchedProd = DB.GetCollection<Products>("Products").Find(product => product.Id == input.Id).FirstOrDefault();
            return searchedProd;
        }
        //Update product
        public static void UpdateDetails(FilterDefinition<Products> filter, UpdateDefinition<Products> update)
        {
            var collection = DB.GetCollection<Products>("Products");
            collection.UpdateOne(filter, update);

        }

                                          //Orders//

        // This method will return all exist orders 
        public static IMongoCollection<Orders> GetOrdersCollection()
        {
            var collection = DB.GetCollection<Orders>("Orders");
            return collection;
        }
        //Place an new order
        public static void AddOrder(Orders order)
        {
            var collection = DB.GetCollection<Orders>("Orders");
            collection.InsertOne(order);
        }
        // Search for all user orders
        public static List<Orders> FindUserOrders(User user)
        {
            List<Orders> searchedorders = DB.GetCollection<Orders>("Orders")
                .Find(order => order.User.ID == user.ID).ToList<Orders>();
            return searchedorders;
        }
    }
}

〜用户模型:

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SurffingSite.Models
{
    public class User
    {
        [Required]
        [BsonId]
        public ObjectId ID { get; set; }

        [BsonElement("FirstName")]
        public String FirstName { get; set; }
        [BsonElement("LastName")]
        public String LastName { get; set; }
        [BsonElement("Phone")]
        public String Phone { get; set; }
        [BsonElement("Email")]
        public String Email { get; set; }
        [BsonElement("Password")]
        public String Password { get; set; }
        [BsonElement("Permission")]
        public string Permission { get; set; }
    }
}

我认为失败是在“过滤器”......

感谢提前

0 个答案:

没有答案