だから私は自分のレコード コレクションをカタログ化したいと考えています。MVCも学びたいです。そこで、MVC でレコードのカタログ Web サイトを構築することにしました。それが私の働き方です。
つま先を浸しているだけですが、複数のファイルを SQLCE データベースにアップロードする方法がわかりません。ここでオプションを選択できます-画像をBLOBまたは単にファイル名として保存し、画像をファイルシステムにアップロードします。
私の単純なモデルはこれです:
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>
私の Create メソッドは次のとおりです。
[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);
}
ただし、画像 (Create メソッドのパラメーター) は常に null で、Model.IsValid は常に false です。
どうしてこれなの?画像のアップロード入力に「image」、「Image」、「image[n]」という名前を付けてみましたが、画像は常に 0 です。
これにはプラグインを使用したくありません。単純なネイティブ MVC の方法はありますか? ヘルパー募集中です!
前もって感謝します。