アップロードフォームがあり、画像やその他のフィールドなどの情報を渡したいのですが、画像をアップロードする方法がわかりません。
これは私のコントローラーコードです:
[HttpPost]
public ActionResult Create(tblPortfolio tblportfolio)
{
if (ModelState.IsValid)
{
db.tblPortfolios.AddObject(tblportfolio);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tblportfolio);
}
そしてこれは私のビューコードです:
@model MyApp.Models.tblPortfolio
<h2>Create</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>tblPortfolio</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ImageFile)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.ImageFile, new { type = "file" })
@Html.ValidationMessageFor(model => model.ImageFile)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Link)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Link)
@Html.ValidationMessageFor(model => model.Link)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
画像をアップロードしてサーバーに保存する方法がわかりません。画像名を設定するにはどうすればよいですかGuid.NewGuid();
。または、どうすれば画像パスを設定できますか?