File upload 绑定复杂ViewModels的名称约定是什么?

File upload 绑定复杂ViewModels的名称约定是什么?,file-upload,asp.net-mvc-4,binding,File Upload,Asp.net Mvc 4,Binding,我有这两个视图模型 public class AboutViewModel : ViewModel { public override long Id { get; set; } public override string PageTitle { get; set; } public override string TitleDescription { get; set; } public override str

我有这两个视图模型

public class AboutViewModel : ViewModel
{
    public override long Id { get; set; }        
    public override string PageTitle { get; set; }        
    public override string TitleDescription { get; set; }        
    public override string ContentTitle { get; set; }
    public virtual AboutItemViewModel AboutItem { get; set; }
}

public class AboutItemViewModel
{
    public long Id { get; set; }

    [AllowHtml]        
    public string Content { get; set; }
    public string ImageUrl { get; set; }
    public HttpPostedFileBase FileToUpload { get; set; }
}
    [ValidateInput(false)]
    [ValidateAntiForgeryToken, HttpPost]
    public ActionResult Create(long? siteid, long? cid, AboutViewModel model)
    {
        return View(model);
    }
这是我的控制器:

public class AboutViewModel : ViewModel
{
    public override long Id { get; set; }        
    public override string PageTitle { get; set; }        
    public override string TitleDescription { get; set; }        
    public override string ContentTitle { get; set; }
    public virtual AboutItemViewModel AboutItem { get; set; }
}

public class AboutItemViewModel
{
    public long Id { get; set; }

    [AllowHtml]        
    public string Content { get; set; }
    public string ImageUrl { get; set; }
    public HttpPostedFileBase FileToUpload { get; set; }
}
    [ValidateInput(false)]
    [ValidateAntiForgeryToken, HttpPost]
    public ActionResult Create(long? siteid, long? cid, AboutViewModel model)
    {
        return View(model);
    }
以下是我的看法:

@using (Html.BeginForm("Create", "About", new { siteid = ViewData["siteid"], cid = ViewData["cid"] },FormMethod.Post,new { enctype = "multipart/form-data", @class = "form-horizontal rtl", autocomplete = "off" }))
{
     <div class="controls">
       <input type="file" name="FileToUpload" id="FileToUpload" style="margin-right: -9px;">
     </div>
     <div class="controls">
        @Html.ValidationMessageFor(o => o.AboutItem.FileToUpload, "", new { id = "spanfile", @class = "alert alert-block alert-error span3 pull-right", style = "margin-right: 160px;" })
     </div>   
     <div class="control-group pull-left">
        <button type="submit" class="btn btn-large" data-toggle="button">Save</button>
    </div> 
}
@使用(Html.BeginForm(“创建”、“关于”、新的{siteid=ViewData[“siteid”]、cid=ViewData[“cid”]}、FormMethod.Post、新的{enctype=“multipart/form data”、@class=“form horizontal”、autocomplete=“off”}))
{
@Html.ValidationMessageFor(o=>o.AboutItem.FileToUpload,”,新的{id=“spanfile”,@class=“警报块警报错误span3向右拉”,style=“margin right:160px;”)
拯救
}
如何将文件绑定到
FileToUpload
以停止返回空值

除外:

如果我把它放在主视图AboutViewModel中,它会返回一个正确的值。

因为FileToUpload属性位于AboutItem属性中,这是父视图模型的一个类属性,您需要在输入元素的名称前面加上它来自的属性。要说文件输入的名称应该是AboutItem.FileToUpload,还有很长的路要走

<input type="file" name="AboutItem.FileToUpload" id="AboutItem_FileToUpload" />
这应该在HTML中呈现

<input type="text" name="AboutItem.Id" id="AboutItem_Id />

该死,伙计,看起来不错。你有我一直在寻找的部分:HttpPostedFileBase名称与文件名匹配,enctype=“multipart/form data”如果缺少其他内容,我看不到,我已经查找了所有内容,我认为我遗漏了一些内容,显然我没有:)酷!虽然我在没有身份证的情况下尝试过,但效果非常奇怪,而且没有效果。很奇怪!