3

ASP.NET MVC 4 と MongoDB を使用して、基本的な映画データベースを作成しようとしています。私の問題は、私の MovieController の POST Update メソッドにあります。

[HttpPost]
    public ActionResult Update(Movie movie)
    {
        if (ModelState.IsValid)
        {

            _movies.Edit(movie);

            return RedirectToAction("Index");
        }

        return View();
    }

ModelState には、ムービーの Id フィールド (ObjectId オブジェクト) のエラーが含まれており、次の例外がスローされます。

 {System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'MongoDB.Bson.ObjectId' failed because no type converter can convert between these types

これは更新ビューです。

@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>
}

モデルの Movie クラス:

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; }
    }
}

編集: ソリューション [ScaffoldColumn(false)] を Id に追加して、ブラウザがレンダリングを試行しないようにしました。ただし、正しい ID を渡すには、Mihai が提供するソリューションを実装する必要がありました。

ObjectId オブジェクトの代わりに文字列を送信しようとするため、ビューで問題が発生していると想定しています。しかし、これを修正する方法がわかりません。何かアイデアはありますか?

4

3 に答える 3

9

この投稿から、この回答を探している他の人にとっては、完全に機能します: http://www.joe-stevens.com/2011/06/12/model-binding-mongodb-objectid-with-asp-net-mvc/

モデル バインダーを作成します。

public class ObjectIdBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        return new ObjectId(result.AttemptedValue);
    }
}

次に、 app start に登録します。

protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdBinder());
}
于 2012-11-10T20:40:01.787 に答える
3

問題は、MVC が Id を ObjectId 型に変換する方法を認識していないことです。文字列としてのみ認識されます。

メソッドにはカスタム バインダーを使用する必要があります。このリンクを見てください http://www.dotnetcurry.com/ShowArticle.aspx?ID=584

これを見てください

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;
    }
}

そして、 Update メソッドをそのように変更します

public ActionResult Update([ModelBinder(typeof(MovieModelBinder))] Movie movie)
于 2012-11-09T11:28:08.493 に答える
1

独自のカスタム型コンバーターを作成する必要があるようです。
このディスカッションをチェックしてください: ObjectId Type Converter

于 2012-11-09T11:32:55.117 に答える