Asp.net mvc 在MVC5中保存对象列表

Asp.net mvc 在MVC5中保存对象列表,asp.net-mvc,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 5,以下是我的两个连接模型。 当我使用单个发票和多个发票详细信息条目发布数据时, 我在Invoice对象中找到了Invoice\u详细信息空白。 我使用的是EditorTemplate,您可以在下面的代码中看到。 如何保存单个发票和多个发票详细信息条目 发票 public class Invoice { [UIHint("Invoice_Details")] public virtual List<Invoice_Details> Invoice_Details { ge

以下是我的两个连接模型。 当我使用单个
发票
和多个
发票详细信息
条目发布数据时, 我在
Invoice
对象中找到了
Invoice\u详细信息
空白。 我使用的是EditorTemplate,您可以在下面的代码中看到。 如何保存单个
发票
和多个
发票详细信息
条目

发票

public class Invoice
{
    [UIHint("Invoice_Details")]
    public virtual List<Invoice_Details> Invoice_Details { get; set; }
    public Invoice()
    {
        Invoice_Details = new List<Models.Invoice_Details>();
    }
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

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

    [Key]
    [Display(Name = "Invoice no")]
    [Required]
    public int INVOICENO { get; set; }

    [Display(Name = "Party")]
    public int PARTY { get; set; }

    [Display(Name = "Broker")]
    public int BROKER { get; set; }

    [Display(Name = "Terms")]
    [Required]
    public int TERMS { get; set; }

    [Display(Name = "Brokerage")]
    [Required]
    public decimal BROKERAGE { get; set; }

    [Display(Name = "Article")]
    [Required]
    public string ARTICLE { get; set; }

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

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

    [ScaffoldColumn(false)]
    public DateTime CREATE_TIMESTAMP { get; set; }
    [ScaffoldColumn(false)]
    public DateTime LAST_EDIT_TIMESTAMP { get; set; }


}
@model SKUMAR.Models.Invoice_Details

<table class="table table-bordered table-hover">


        <tr>
            <td>@Html.TextBoxFor(m=>m.CARET, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.PRICE, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.RUPEESDOLLAR, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.RATEIFDOLLAR, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.TOTAL, new { @class = "form-control" })</td>
        </tr>

</table>
创建视图

@model SKUMAR.Models.Invoice

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 

{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Invoice</h4>
    <hr />
    <div class="col-md-6">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.INVOICENO, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.INVOICENO, new { htmlAttributes = new {@autofocus="autofocus", @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.INVOICENO, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DATE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.DATE,new {@Value=ViewBag.CURRENTDATE, @class = "form-control" } )
                @Html.ValidationMessageFor(model => model.DATE, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.DETAILS, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DETAILS, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DETAILS, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="col-md-6">
        @Html.EditorFor(m=>m.Invoice_Details)
    </div>
    <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>
@model SKUMAR.Models.Invoice
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
发票联

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.INVOICENO,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.INVOICENO,new{htmlAttributes=new{@autofocus=“autofocus”,@class=“form control”}}) @Html.ValidationMessageFor(model=>model.INVOICENO,“,new{@class=“text danger”}) @LabelFor(model=>model.DATE,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.TextBoxFor(model=>model.DATE,新的{@Value=ViewBag.CURRENTDATE,@class=“form control”}) @Html.ValidationMessageFor(model=>model.DATE,“,new{@class=“text danger”}) @LabelFor(model=>model.PARTY,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.PARTY,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.PARTY,“,new{@class=“text danger”}) @LabelFor(model=>model.BROKER,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.BROKER,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.BROKER,“,new{@class=“text danger”}) @LabelFor(model=>model.TERMS,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.TERMS,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.TERMS,“,new{@class=“text danger”}) @LabelFor(model=>model.BROKERAGE,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.BROKERAGE,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.BROKERAGE,“,new{@class=“text danger”}) @LabelFor(model=>model.ARTICLE,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ARTICLE,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.ARTICLE,“,new{@class=“text danger”}) @LabelFor(model=>model.TOTAL,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.TOTAL,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.TOTAL,“,new{@class=“text danger”}) @LabelFor(model=>model.DETAILS,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.DETAILS,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.DETAILS,“,new{@class=“text danger”}) @EditorFor(m=>m.Invoice\u详细信息) } @ActionLink(“返回列表”、“索引”)
发票详情.cshtml编辑模板

public class Invoice
{
    [UIHint("Invoice_Details")]
    public virtual List<Invoice_Details> Invoice_Details { get; set; }
    public Invoice()
    {
        Invoice_Details = new List<Models.Invoice_Details>();
    }
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

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

    [Key]
    [Display(Name = "Invoice no")]
    [Required]
    public int INVOICENO { get; set; }

    [Display(Name = "Party")]
    public int PARTY { get; set; }

    [Display(Name = "Broker")]
    public int BROKER { get; set; }

    [Display(Name = "Terms")]
    [Required]
    public int TERMS { get; set; }

    [Display(Name = "Brokerage")]
    [Required]
    public decimal BROKERAGE { get; set; }

    [Display(Name = "Article")]
    [Required]
    public string ARTICLE { get; set; }

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

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

    [ScaffoldColumn(false)]
    public DateTime CREATE_TIMESTAMP { get; set; }
    [ScaffoldColumn(false)]
    public DateTime LAST_EDIT_TIMESTAMP { get; set; }


}
@model SKUMAR.Models.Invoice_Details

<table class="table table-bordered table-hover">


        <tr>
            <td>@Html.TextBoxFor(m=>m.CARET, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.PRICE, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.RUPEESDOLLAR, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.RATEIFDOLLAR, new { @class = "form-control" })</td>
            <td>@Html.TextBoxFor(m => m.TOTAL, new { @class = "form-control" })</td>
        </tr>

</table>
@model SKUMAR.Models.Invoice\u详细信息
@TextBoxFor(m=>m.CARET,新的{@class=“form control”})
@TextBoxFor(m=>m.PRICE,新的{@class=“form control”})
@TextBoxFor(m=>m.rupesdaular,新的{@class=“form control”})
@TextBoxFor(m=>m.RATEIFDOLLAR,新的{@class=“form control”})
@TextBoxFor(m=>m.TOTAL,新的{@class=“form control”})

Invoice\u Details.cshtml EditorTemplate
中使用
@model SKUMAR.Models.Invoice\u Details
,而不是
@model SKUMAR.Models.Invoice
,然后在此基础上更改每一行。例如,将
@Html.TextBoxFor(m=>m.CARET
更改为
@Html.TextBoxFor(m=>m.Invoice\u Details.CARET

从模型中删除
[UIHint(“Invoice\u Details”)]
,并确保
Invoice\u Details.cshtml
文件位于
/Views/Shared/editoremplates
文件夹中(或
/Views/YourController/EditorTemplates
文件夹)-只要您看到
名称
属性,如
name=“Invoice\u Details[0]。插入符号“
等,它就会绑定良好