---
title: "ModelState.IsValid contains errors when using MongoDB"  
description: "ModelState.IsValid contains errors when using MongoDB"  
author: "Anonymous User"  
published: 2014-03-29  
updated: 2014-03-29  
canonical: https://www.mindstick.com/forum/2002/modelstate-isvalid-contains-errors-when-using-mongodb  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# ModelState.IsValid contains errors when using MongoDB

I'm trying to create a basic movie [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) using [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) 4 and MongoDB. My [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is in the POST [Update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) method of my MovieController.

```
[HttpPost]    public ActionResult Update(Movie movie)    {        if (ModelState.IsValid)        {           
_movies.Edit(movie);            return RedirectToAction("Index");        }        return View();    }
```

The ModelState contains an error for the movie's Id field (which is an ObjectId object) and throws the following [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling):

{System.InvalidOperationException: The [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) [conversion](https://www.mindstick.com/forum/1734/conversion-from-32-bit-integer-to-4-chars) from type 'System.[String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)' to type 'MongoDB.Bson.ObjectId' failed because no type converter can [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) between these types

## This is the Update view:

```
@model MVCMovie.Models.Movie@{    ViewBag.Title = "Update";}<h2>Update</h2>@using (Html.BeginForm()){    @Html.HiddenFor(m => m.Id);    @Html.EditorForModel()    <p>        <input type="submit" value="Update" />    </p>}
```

And the Movie class in Models:

```
namespace MVCMovie.Models{    public class Movie    {        [BsonId]        public ObjectId Id { get; set; }        public string Title { get; set; }        public DateTime ReleaseDate { get; set; }        public string Genre { get; set; }        public decimal Price { get; set; }        [ScaffoldColumn(false)]        public DateTime TimeAdded { get; set; }    }}
```

I'm assuming that the problem is being caused in the view because it tries to send a string instead of an ObjectId object. But I cannot figure out how to fix this, any ideas?

## Replies

### Reply by Pravesh Singh

Hi Samuel,

Have a look at this

```
public class MovieModelBinder : IModelBinder{    public object BindModel(ControllerContext controllerContext, ModelBindingContext
bindingContext)    {        var modelBinder = new DefaultModelBinder();        var movie = modelBinder.BindModel(controllerContext, bindingContext) as Movie;        var id = controllerContext.HttpContext.Request.Form["Id"];        if (movie != null)        {            movie.Id = new ObjectId(id);            return
movie ;        }        return null;    }}
```

And change your Update method as so

public ActionResult Update([ModelBinder(typeof(MovieModelBinder))] Movie movie)


---

Original Source: https://www.mindstick.com/forum/2002/modelstate-isvalid-contains-errors-when-using-mongodb

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
