私は自分のレコードコレクションをカタログしたいと思います。私はまた、いくつかのMVCを学びたい。そこで、私はMVCにレコードカタログ作成ウェブサイトを構築することにしました。それが私の仕事です。モデルバインディングを使用してMVC3で複数のファイルをアップロード
私は自分の足元をつかんでいますが、複数のファイルをSQLCEデータベースにアップロードする方法を理解できません。私はここにオプションを用意しています - イメージをBLOBSとして保存するか、単にファイル名として保存し、イメージをファイルシステムにアップロードします。私のビューがある
public class Record
{
[ScaffoldColumn(false)]
public int RecordId { get; set; }
[Required(ErrorMessage = "Artist is required")]
public string Artist { get; set; }
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
[DisplayName("Release Date")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime ReleaseDate { get; set; }
[Required(ErrorMessage = "Format is required")]
public string Format { get; set; }
public string Label { get; set; }
[DisplayName("Catalogue Number")]
public string CatalogueNumber { get; set; }
public string Matrix { get; set; }
public string Country { get; set; }
public IEnumerable<HttpPostedFileBase> Images { get; set; }
public string Notes { get; set; }
}
:
私の単純なモデルがこれです
@model Records.Models.Record
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Record</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Artist)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Artist)
@Html.ValidationMessageFor(model => model.Artist)
</div>
// snipped for brevity
<div class="editor-label">
@Html.LabelFor(model => model.Notes)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Notes)
@Html.ValidationMessageFor(model => model.Notes)
</div>
<div class="editor-field">
<input type="file" name="images" id="image1"/>
<input type="file" name="images" id="image2"/>
<input type="file" name="images" id="image3"/>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
、私のメソッドを作成します:
[HttpPost]
public ActionResult Create(Record record, IEnumerable<HttpPostedFileBase> images)
{
if (ModelState.IsValid)
{
foreach (HttpPostedFileBase image in images)
{
if (image.ContentLength > 0)
{
var fileName = Path.GetFileName(image.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
image.SaveAs(path);
}
}
db.Records.Add(record);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(record);
}
しかし、画像(my Createメソッドのparam)は常にnullであり、Model.IsValidは常にfalseです。
これはなぜですか?イメージアップロードの名前を 'イメージ'、 'イメージ'、 'イメージ[n]'、画像は常に0にしてみました。
私は本当にこのプラグインを使用したくありません。単純なネイティブMVCの方法?私はヘルパーに開放されています!
ありがとうございます。
について完璧なブログを持っていますか?私はあなたが[HttpPost]部分を持っているのを見ます。 –
@Geovani、はい私はビューを表示するだけの[HttpGet]メソッドを持っています。 – Brett
HTTPGETでは、レコードのインスタンスを返すか、通常の戻り値のView()を返しますか?質問を編集してHTTGETメソッドのコードを投稿できますか? –