我有一个上传表单,我想传递一些信息,例如图片和其他字段,但我不知道如何上传图片..
这是我的控制器代码:
[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();
来设置图像名称?或者我如何设置图像路径?
model.ImageFile
的类型是什么? - Shimmy Weitzhandler