恢复模式
public class Resume
{
public string Name { get; set; }
public byte[] Binary { get; set; }
public int PageCount { get; set; }
public string Path { get; set; }
}
联系模式
public class Contact
{
public string Name { get; set; }
public string ContactNumber { get; set; }
public string Address{ get; set; }
}
学生模特
public class Student
{
public List<Contact> Contacts{ get; set; }
public List<Resumes> Resume{ get; set; }
}
这是视图
@model SendAFaxWeb.Models.Student
@{
ViewBag.Title = "Upload";
}
@using (Html.BeginForm("Submit", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ @Html.AntiForgeryToken()
<div>
@Html.LabelFor(x => x.Contact.Name)
@Html.TextBoxFor(x => x.Contact.Name)
@Html.LabelFor(x => x.Contact.ContactNumber)
@Html.TextBoxFor(x => x.Contact.ContactNumber)
@Html.LabelFor(x => x.Contact.Address)
@Html.TextBoxFor(x => x.Contact.Address)
*//here should be upload resume, the detail will be save in Resumes model(name, binary, pageCount and path)*
@Html.Label("Upload Resume")
@Html.TextBoxFor(x => x.file, new { type = "file" })
</div>
<button type="submit">Upload</button>
}
控制器
public ActionResult Submit(Student stud)
{
//to save student details
}
抱歉我的英语不好,我是MVC开发的新手。我的问题是我不知道如何将带有其他数据的文件传递给控制器。控制器期望 Student stud 拥有所有数据恢复和联系。我该如何设法解决这个问题?
答案 0 :(得分:0)
如果我认为您的代码是良好的做法:尝试将其添加到您的学生班:
public HttpPostedFileBase FileToUpload { get; set;}
在视图中更改行:
@Html.TextBoxFor(x => x.FileToUpload, new { type = "file" })
然后在控制器中你可以这样做:
[HttpPost]
public ActionResult Index(Student model)
{
var file= model.FileToUpload;
if (file != null && file.ContentLength > 0)
{
// extract only the filename
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
但是,如果您考虑重新设计可能导致更好的可管理性的代码,请研究视图模型概念。谷歌先生可以为您提供很多帮助。希望这有帮助