保存MVC 5中的对象列表

时间:2017-01-12 14:04:56

标签: asp.net-mvc asp.net-mvc-5

以下是我的两个连接型号。 当我使用单个Invoice和多个Invoice_Details条目发布数据时, 我在Invoice_Details对象中发现Invoice空白。 我正在使用EditorTemplate,您可以在下面的代码中看到。 如何保存单个Invoice和多个Invoice_Details条目。

发票

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; }


}

Invoice_Details

public class Invoice_Details
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

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

    [Display(Name = "Caret")]
    public decimal CARET { get; set; }

    [Display(Name = "Price")]
    public decimal PRICE { get; set; }

    [Display(Name = "Rs/Dollar")]
    [Required]
    public string RUPEESDOLLAR { get; set; }

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

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

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

    public virtual Invoice invoice { get; set; }
}

创建视图

@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>

Invoice_Details.cshtml EditorTemplate

@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>

1 个答案:

答案 0 :(得分:0)

function Send-Email { Param( [Parameter(Mandatory=$true)] [string]$BodyString, [Parameter(Mandatory=$true)] [string]$SubjectString, [Parameter(Mandatory=$true)] [string[]]$EmailRecipientsArray, [Parameter(Mandatory=$false)] [string]$FileAttachment = '', [Parameter(Mandatory=$false)] [AllowEmptyString()] [string]$BlindCopy = '' ) try { $user = "user@foo.com" $pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force $cred = New-Object Management.Automation.PSCredential $user, $pass $params = @{ 'From' = 'foo@bar.org' 'To' = 'bar@foo.org' 'Subject' = $SubjectString 'Body' = $BodyString 'Priority' = 'High' 'dno' = 'onSuccess', 'onFailure' 'SmtpServer' = 'some.mail.server' 'Credential' = $cred } if ($BlindCopy) { $params['Bcc'] = $BlindCopy } if($FileAttachment) { $params['Attachments'] = $FileAttachment } Send-MailMessage @params -BodyAsHtml } catch { ... } } 内而不是-BlindCopy,使用Invoice_Details.cshtml EditorTemplate,然后根据该行更改每一行。例如,将@model SKUMAR.Models.Invoice_Details更改为@model SKUMAR.Models.Invoice