質問

このようなMVC4でImagesControllerを作成しようとしています

enter image description here

しかし、私はこのエラーを取得し続けます。

enter image description here

このクラスを使用してPeopleControllerのコントローラーの作成に問題がなかった

public class Person
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public virtual IEnumerable<Affair> Affairs { get; set; }
}
役に立ちましたか?

解決

問題はあなたのことです File 上のプロパティ Image クラス。 EntityFrameworkがタイプを理解しないためです HttpPostedFileBase また、DBに保存することはできず、コントローラージェネレーターはこれを確認するのに十分スマートです。 altoughエラーメッセージは何が問題なのかわかりません。これを修正するには、バイト配列を使用するようにプロパティを書き換える必要があります。

public class Image
{
    ...
    public byte[] File { get; set; }
}

そして、コントローラーの生成が機能するはずです。そして、あなたはあなた自身の画像をアップロードするアクションを追加することができます、次のようなもの:

[HttpPost]
public ActionResult Upload(Image image, HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        db.Entry(image).State = EntityState.Modified;
        image.File = new byte[file.ContentLength];
        file.InputStream.Read(image.File, 0, file.ContentLength); 
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(image);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top