0

モデルアイテムを持っています

public class EntryInputModel
{
    ...

    [Required(ErrorMessage = "Description is required.", AllowEmptyStrings = false)]
    public virtual string Description { get; set; }
}

とコントローラーアクション

public ActionResult Add([Bind(Exclude = "Id")] EntryInputModel newEntry)
{
    if (ModelState.IsValid)
    {
        var entry = Mapper.Map<EntryInputModel, Entry>(newEntry);

        repository.Add(entry);
        unitOfWork.SaveChanges();
        return RedirectToAction("Details", new { id = entry.Id });
    }
    return RedirectToAction("Create");
}

EntryInputModel単体テストでを作成し、Descriptionプロパティをに設定nullしてアクションメソッドに渡すと、ModelState.IsValid == trueデバッグして検証したにもかかわらず、が取得されnewEntry.Description == nullます。

なぜこれが機能しないのですか?

4

1 に答える 1

0

これは、テストからアクションを呼び出すときにモデルのバインドが行われないためです。モデルバインディングは、投稿されたフォーム値をタイプにマッピングし、それをパラメーターとしてアクションメソッドに渡すプロセスです。

于 2010-04-06T03:15:08.450 に答える