Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MongoDB:MVC未在post上序列化BsonId_C#_Asp.net Mvc 3_Mongodb_Mongodb .net Driver - Fatal编程技术网

C# MongoDB:MVC未在post上序列化BsonId

C# MongoDB:MVC未在post上序列化BsonId,c#,asp.net-mvc-3,mongodb,mongodb-.net-driver,C#,Asp.net Mvc 3,Mongodb,Mongodb .net Driver,我无法使用MongoDB csharp官方库获取模型返回值的ObjectId。当我提交发回控制器的表单时,PageID总是返回到模型中的{000000000000000000000000000000000000}。呈现的HTML具有有效id,即一个字段永远不会从表单post正确返回 html呈现: 这就是我所拥有的 型号: public class PageModel { public PageModel() { CanEdit =

我无法使用MongoDB csharp官方库获取模型返回值的ObjectId。当我提交发回控制器的表单时,PageID总是返回到模型中的{000000000000000000000000000000000000}。呈现的HTML具有有效id,即一个字段永远不会从表单post正确返回

html呈现:

这就是我所拥有的

型号:

public class PageModel
    {
        public PageModel()
        {
            CanEdit = false;
            CancelRequest = false;
        }

        [BsonId]
        public ObjectId PageID { get; set; }

        [Display(Name = "Page URL")]
        public string PageURL { get; set; }

        [AllowHtml]
        [Display(Name = "Content")]
        public string Value { get; set; }

        [Display(Name = "Last Modified")]
        public DateTime LastModified { get; set; }

        [BsonIgnore]
        public bool CancelRequest { get; set; }
        [BsonIgnore]
        public bool CanEdit { get; set; }

    }
主计长职位:

[HttpPost]
        public ActionResult Edit(PageModel model)
        {
            // check to see if the user canceled
            if (model.CancelRequest)
            {
                return Redirect(model.PageURL);
            }

            // check for a script tag to prevent
            if (!model.Value.Contains("<script"))
            {
                // save to the database
                model.LastModified = DateTime.Now;
                collection.Save(model);

                // return to the page
                return Redirect(model.PageURL);
            }
            else
            {
                ModelState.AddModelError("Value", "Disclosures discovered a script in the html you submitted, please remove the script before saving.");
            }

            return View(model);
        }
视图:


看起来您正在为PageID发送一个字符串值,您希望它是ObjectId类型的对象

模型绑定不知道如何将这个字符串转换为ObjectId。如果您查看MongoDriver中的ObjectId类,就会发现它非常复杂

我们经常使用mongodb,对于我们的id,我们只使用字符串,这提供了很大的灵活性。我不确定您需要ObjectId类作为文档Id的用例,但您可能不需要它

从这里看来,你有两个选择

将文档id更改为字符串或其他内容 为ObjectId类编写自定义模型绑定器
希望有帮助:

看起来您正在为PageID发送一个字符串值,您希望它是ObjectId类型的对象

模型绑定不知道如何将这个字符串转换为ObjectId。如果您查看MongoDriver中的ObjectId类,就会发现它非常复杂

我们经常使用mongodb,对于我们的id,我们只使用字符串,这提供了很大的灵活性。我不确定您需要ObjectId类作为文档Id的用例,但您可能不需要它

从这里看来,你有两个选择

将文档id更改为字符串或其他内容 为ObjectId类编写自定义模型绑定器 希望有帮助:

创建绑定:

公共类objectibinder:IModelBinder {

}

创建ModelBinderConfig:

名称空间Nasa8x.CMS { 公共类ModelBinderConfig { 公共静态无效注册表ModelBinder字典绑定器 { binder.AddtypeofObjectId,新ObjectDBinder

        binder.Add(typeof(string[]), new StringArrayBinder());

        binder.Add(typeof(int[]), new IntArrayBinder());

    }
}
}

在Global.cs上注册:

 protected void Application_Start()
            {  

                ModelBinderConfig.Register(ModelBinders.Binders);

                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);    

            }
创建绑定:

公共类objectibinder:IModelBinder {

}

创建ModelBinderConfig:

名称空间Nasa8x.CMS { 公共类ModelBinderConfig { 公共静态无效注册表ModelBinder字典绑定器 { binder.AddtypeofObjectId,新ObjectDBinder

        binder.Add(typeof(string[]), new StringArrayBinder());

        binder.Add(typeof(int[]), new IntArrayBinder());

    }
}
}

在Global.cs上注册:

 protected void Application_Start()
            {  

                ModelBinderConfig.Register(ModelBinders.Binders);

                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);    

            }

如果您不需要C类中的PageID属性为ObjectId类型,但希望在MongoDB端利用此类型,则可以让驱动程序在类定义中处理转换:

public class PageModel
{

    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string PageID { get; set; }

}

如果您不需要C类中的PageID属性为ObjectId类型,但希望在MongoDB端利用此类型,则可以让驱动程序在类定义中处理转换:

public class PageModel
{

    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string PageID { get; set; }

}

谢谢你提供的信息。我在MVC方面很有经验,但对MongoDB来说是新手。你的第二个选择实际上给了我足够的信息,我能够创建一个模型绑定器来帮我解决这个问题。下面是一个例子。谢谢你提供的信息。我在MVC方面很有经验,但对MongoDB来说是新手。你的第二个选择实际上给了我一个enough信息继续,我能够创建一个模型绑定器来处理这个问题。下面是一个示例。