这是我的模特:
public string ALTCLC { get; set; }
public string ALTGEN { get; set; }
public int ALTCNIC { get; set; }
public DateTime ALTDOB { get; set; }
[Required]
public string ALTName { get; set; }
这是我的控制器名称= AdultLiteracy控制器 每件事都是EF,但我写的是我的班级名称上传,这是无效的
namespace LiteracyPayroll.Controllers
{
public class AdultLiteracyTeachersController : Controller
{
private PayrollDBContext db = new PayrollDBContext();
//
// GET: /AdultLiteracyTeachers/
public ActionResult Index()
{
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase[] files)
{
foreach (HttpPostedFileBase file in files)
{
string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
}
ViewBag.Message = "File(s) uploaded successfully";
return RedirectToAction("Index");
}
//
// GET: /AdultLiteracyTeachers/Details/5
public ActionResult Details(int id = 0)
{
AdulLiteracyTeachers adulliteracyteachers = db.AdulLiteracyTeachers.Find(id);
if (adulliteracyteachers == null)
{
return HttpNotFound();
}
return View(adulliteracyteachers);
}
/
这是我的观点:
@model LiteracyPayroll.Models.AdulLiteracyTeachers
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AdulLiteracyTeachers</legend>
<div class="editor-label">
@Html.LabelFor(model => model.DistID, "District")
</div>
<div class="editor-field">
@Html.DropDownList("DistID", String.Empty)
@Html.ValidationMessageFor(model => model.DistID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ALTCLC)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ALTCLC)
@Html.ValidationMessageFor(model => model.ALTCLC)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ALTGEN)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ALTGEN)
@Html.ValidationMessageFor(model => model.ALTGEN)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ALTCNIC)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ALTCNIC)
@Html.ValidationMessageFor(model => model.ALTCNIC)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ALTDOB)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ALTDOB)
@Html.ValidationMessageFor(model => model.ALTDOB)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ALTName)
</div>
<label for="file">Upload Image:</label>
<input type="file" name="files" value="Upload Image" />
<input name="Upload" type="submit" value="Create" />
</p>
</fieldset>
当我按下我的创建按钮时,我遇到的问题是值传递到数据库但图像文件未发布 如何在现有控制器类中调用“我上传我的动作”结果?
答案 0 :(得分:0)
对于表单中的文件发布,您必须像这样制作表单:
@using (Html.BeginForm("ACTION", "CONTROLLER", FormMethod.Post, new { EncType="multipart/form-data"})){
......
.....
}
并在行动中:
[HttpPost]
public ActionResult ACTION(AdulLiteracyTeachers model, HttpPostedFileBase file)
{
}
或者您可以从请求中读取文件:
[HttpPost]
public ActionResult ACTION(AdulLiteracyTeachers model)
{
foreach (string requestFile in Request.Files)
{
HttpPostedFileBase file = Request.Files[requestFile];
if (file.ContentLength > 0)
{
}
}
}