Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC5中,视图的两个模型中的一个总是空的_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 在MVC5中,视图的两个模型中的一个总是空的

C# 在MVC5中,视图的两个模型中的一个总是空的,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,在我的MVC5应用程序中,我有两个型号订单和文件,如下所示: public class Order { public int OrderID { get; set; } public string OrderName{ get; set; } } public class File { public HttpPostedFileBase[] files { get; set; } } 我希望在单个视图中编辑两个类的对象,因此我创建父类: public class

在我的MVC5应用程序中,我有两个型号订单文件,如下所示:

public class Order
{
    public int OrderID { get; set; }
    public string OrderName{ get; set; }
}

public class File
{
    public HttpPostedFileBase[] files { get; set; }  
}
我希望在单个视图中编辑两个类的对象,因此我创建父类:

public class MainContext
{
    public Order Order { get; set; }
    public File File { get; set; }
}
我认为:

@using (Html.BeginForm("Create", "Order", FormMethod.Post, new { encType = "multipart/form-data" }))
@Html.AntiForgeryToken()

<div class="form-group">
    <label>OrderName</label>
    @Html.EditorFor(model => model.Order.OrderName, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Order.OrderName, "", new { @class = "text-danger" })
</div>

<div class="form-group">
    @Html.LabelFor(model => model.File.files, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.File.files, "", new { @type = "file", @multiple = "multiple", })
        @Html.ValidationMessageFor(model => model.File.files, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <input type="submit" value="submit" class="btn btn-success btn-lg btn-block" />
</div>
现在问题解决了。。提交表单后,我在Create action中获得了订单值,但文件始终为空

我想念什么

提前谢谢

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MainContext model)
{
  // here fetch values from model.Order and model.File
}
不是在post操作中分别获取两个模型,而是调用“MainContext”类,从中可以获取所有视图值

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MainContext model)
{
  // here fetch values from model.Order and model.File
}