C# 尝试上载包含数据的文件时,viewmodel中的值为空
我正在尝试创建一个视图,该视图允许用户上载Office文件以及包含URL、类型等的类。我当前使用的是此视图模型:C# 尝试上载包含数据的文件时,viewmodel中的值为空,c#,asp.net-mvc,razor,model-binding,C#,Asp.net Mvc,Razor,Model Binding,我正在尝试创建一个视图,该视图允许用户上载Office文件以及包含URL、类型等的类。我当前使用的是此视图模型: public class ServiceFileUploadViewModel { public FileUpload File { get; set; } public IList<Service> AvalibleServices { get; set; } public IList<Service> SelectedService
public class ServiceFileUploadViewModel
{
public FileUpload File { get; set; }
public IList<Service> AvalibleServices { get; set; }
public IList<Service> SelectedServices { get; set; }
public PostedServices PostedServices { get; set; }
public HttpPostedFileBase FileUpload { get; set; }
}
public class PostedServices
{
public int[] ServicesIds { get; set; }
}
公共类ServiceFileUploadViewModel
{
公共文件上载文件{get;set;}
公共IList可用服务{get;set;}
公共IList SelectedServices{get;set;}
公共PostedServices PostedServices{get;set;}
公共HttpPostedFileBase文件上载{get;set;}
}
公共邮递服务
{
public int[]ServicesIds{get;set;}
}
我正在使用一个名为“”的扩展名,它创建一个复选框列表,将文件链接到特定的服务
以下是我对表格的看法:
@model ServiceFileUploadViewModel
@using (Html.BeginForm("_Upload", "ServiceCatalog", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="file" />
</div>
<div>
Dokumenttyp
</div>
<div>
@Html.EditorFor(m => m.File.DocumentType)
</div>
<div>
@Html.CheckBoxListFor(m => m.PostedServices.ServicesIds,
m => m.AvalibleServices,
entity => entity.Id,
entity => entity.Name,
m => m.SelectedServices,
Position.Vertical)
</div>
<div>
<label>Visningsalternativ</label>
</div>
<div>
@Html.RadioButtonFor(m => m.File.IsPrivate, "true", true) Interninformation
</div>
<div>
@Html.RadioButtonFor(m => m.File.IsPrivate, "false", false) Kundinformation
</div>
<input type="submit" value="Ladda upp" />
}
@model ServiceFileUploadViewModel
@使用(Html.BeginForm(“_Upload”,“ServiceCatalog”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
Dokumenttyp
@EditorFor(m=>m.File.DocumentType)
@(m=>m.PostedServices.ServicesIds,
m=>m.AvailableService,
entity=>entity.Id,
entity=>entity.Name,
m=>m.SelectedServices,
位置(垂直)
Visningsalternativ
@(m=>m.File.IsPrivate,“true”,true)InternetInformation
@Html.radioButton(m=>m.File.IsPrivate,“false”,false)昆丁信息
}
我的问题是,当我在控制器中的_Upload操作中设置断点时,除了包含复选框列表中所选服务的“PostedServices”数组之外,所有值(文件、FileUpload类)都为空
我一直想弄明白,但没有成功。您知道为什么我的表单不能正确映射到我的viewmodel吗?您的
HttpPostedFileBase FileUpload
属性将为null
,因为您已将输入命名为“file”。这是必须的
<input type="file" name="FileUpload" />.
。
这也意味着FileUpload File
在回发时将为null,因为您尝试将上载文件的值绑定到FileUpload
的类型(当然失败了-因此为null
)
注意:您尚未发布您的文件上载
类,因此无法确定是否存在其他问题
还要注意的是,您应该从
@Html.radiobutton()
中删除第三个参数。您使用的是强类型帮助程序,这意味着所选按钮将基于原来的IsPrivate
值!非常感谢您快速准确的回复!