这是我的视图模型:
public class AgencyViewModel
{
public int VendorId { get; set; }
public IEnumerable<Vendor> Vendors { get; set; }
public HttpPostedFileBase SpreadsheetFile { get; set; }
}
这是我的两个控制器方法:
public ActionResult Upload()
{
var viewModel = new AgencyViewModel
{
Vendors = _vendorsRepository.Vendors.OrderBy(v => v.Name)
};
return View(viewModel);
}
[HttpPost]
public ActionResult Upload(AgencyViewModel viewModel)
{
// do stuff
}
而我的观点:
@model MyModels.AgencyViewModel
@using (Html.BeginForm("Upload", "Agencies", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.DropDownListFor(model => model.VendorId,
new SelectList(Model.Vendors, "VendorID", "Name"),
"- Select a Vendor -")
@Html.TextBoxFor(model => model.SpreadsheetFile, new { type = "file" })
<p>
<input type="submit" value="Next" />
</p>
}
当我导航到Upload
时,Vendors
已正确加载。但是当我提交表单时,我的视图模型只包含VendorId
andSpreadsheetFile
和Vendors
is null
。
提交表单后,我需要对集合进行一些处理Vendors
,并且我不想将所有数据加载两次。
如何将数据保存在我的视图模型中?