Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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# 具有创建视图的ASP.NET MVC嵌套集合_C#_Asp.net Mvc - Fatal编程技术网

C# 具有创建视图的ASP.NET MVC嵌套集合

C# 具有创建视图的ASP.NET MVC嵌套集合,c#,asp.net-mvc,C#,Asp.net Mvc,我有以下模型类: public class TripCreateVM { public TripCreateVM() { Stops = new List<TripStopVM>(); } [Display(Name = "Trip ID")] public int ID { get; set; } [Display(Name = "Driver")] public int? DriverId { get;

我有以下模型类:

public class TripCreateVM
{
    public TripCreateVM()
    {
        Stops = new List<TripStopVM>();
    }

    [Display(Name = "Trip ID")]
    public int ID { get; set; }

    [Display(Name = "Driver")]
    public int? DriverId { get; set; }
    public SelectList DriverList { get; set; }

    [Display(Name = "Account")]
    public int? BrokerId { get; set; }
    public SelectList BrokerList { get; set; }

    [Required]
    [Display(Name = "Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public System.DateTime DateTime { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "Mileage")]
    [DisplayFormat(DataFormatString = "{0:N}")]
    public int? Mileage { get; set; }

    [Display(Name = "Freight Amount")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal? FreightAmount { get; set; }

    [Display(Name = "Pay Amount")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal? Amount { get; set; }

    [Display(Name = "Tolls")]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal? Tolls { get; set; }

    [Required]
    [Display(Name = " ")]
    public bool PaperRecd { get; set; }

    [Display(Name = "Pro #")]
    public string ProNo { get; set; }

    [Display(Name = "Load #")]
    public string LoadNo { get; set; }

    [Display(Name = "Notes")]
    public string Notes { get; set; }

    [Display(Name = "Total Hours")]
    public decimal? Hours { get; set; }

    public List<TripStopVM> Stops { get; set; }
}

public class TripStopVM
{
    public int ID { get; set; }

    [Display(Name = "Stop #")]
    public int StopNo { get; set; }

    [Display(Name = "Company Name")]
    public string CoName { get; set; }

    [Display(Name = "Address")]
    public string Address { get; set; }

    [Display(Name = "City")]
    public string City { get; set; }

    [Display(Name = "State")]
    public string State { get; set; }

    [Display(Name = "Zip Code")]
    public string Zip { get; set; }

    [Display(Name = "Phone / Contact")]
    public string Phone { get; set; }

    [Display(Name = "Appt Time")]
    public DateTime? Appt { get; set; }

    [Display(Name = "Appt Notes")]
    public string ApptNotes { get; set; }

    [Display(Name = "Ref #")]
    public string RefNo { get; set; }

    [Display(Name = "Notes")]
    public string Notes { get; set; }
}

但当调用post方法时,Stops集合为空。如何填充它呢?

您需要在集合上使用带有计数器的for循环。然后,这将为每个选项和属性生成唯一的id,并允许将其绑定到模型

 @for (var count= 0; count <= (Model.Stops.Count - 1); count++)
 {
    @Html.EditorFor(model => model.Stops[count].ID)
    @Html.EditorFor(model => model.Stops[count].CoName)
    @Html.EditorFor(model => model.Stops[count].Address)
 }
希望有帮助

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    bool first = true;
    <div class="container-fluid">
        <div class="row">
            @*<div class="col-md-3">*@
            <div class="row">
                <div class="row_wrapper">


                    <div class="form-horizontal">
                        <h4>TripCreateVM</h4>
                        <hr />
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(model => model.DriverId, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.DropDownListFor(model => model.DriverId, Model.DriverList)
                                @Html.ValidationMessageFor(model => model.DriverId, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <!-- Other model properties -->

                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Create" class="btn btn-default" />
                            </div>
                        </div>
                    </div>


                    <div>
                        @Html.ActionLink("Back to List", "Index")
                    </div>

                </div>
            </div>
            @*<div class="col-md-9">*@
            <div class="row">
                <div class="">
                    <div><a href="#" id="addNew" class="btn btn-success" style="margin-bottom:10px;"><span class="glyphicon glyphicon-plus"></span> Add Stops</a></div>

                    <table id="dataTable" class="table table-bordered table-condensed" style="table-layout:fixed; ">
                        <thead>
                            <tr>
                                <th>Company Name</th>
                                <th>Street</th>
                                <th>City</th>
                                <th style="width:4%">State</th>
                                <th style="width:6%">Zip</th>
                                <th>Phone #</th>
                                <th>Appt Time</th>
                                <th>Ref #</th>
                                <th>Notes</th>
                                <th style="width:5%"></th>
                            </tr>
                        </thead>
                        @foreach (var item in Model.Stops)
                        {
                            <tr>
                                @Html.HiddenFor(model => item.ID)
                                <td>
                                    @Html.TextBoxFor(model => item.CoName)
                                </td>
                                <td>
                                    @Html.EditorFor(model => item.Address)
                                </td>
                                <!-- other TripStop properties -->
                                <td>
                                    <a href="#" class="remove">Remove</a>
                                </td>
                            </tr>
                        }
                    </table>
                </div>
            </div>
        </div>

    </div>
}
    [HttpPost()]
    [ValidateAntiForgeryToken()]
    public ActionResult Create(TripCreateVM model, string ReturnUrl)
    {
 @for (var count= 0; count <= (Model.Stops.Count - 1); count++)
 {
    @Html.EditorFor(model => model.Stops[count].ID)
    @Html.EditorFor(model => model.Stops[count].CoName)
    @Html.EditorFor(model => model.Stops[count].Address)
 }