0

I am using Visual Studio 2015 community for one of my demo project while using ASP.NET MVC 5 and EF6.

I am having following two models and a viewmodel.

Invoice and Invoice_Details models are interconnected by PK and FK relationships.

One Invoice will Contain multiple information in Invoice_Details.

Invoice

public class Invoice
{
    [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; }

    public virtual ICollection<Invoice_Details> Invoice_Detailss { 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; }
}

VIEWMODEL

public class Invoice_Invoice_Details_ViewModel
{
    public Invoice Invoices { get; set; }
    public List<Invoice_Details> Invoice_Detailss { get; set; }
}

This viewmodel contains both models.

CREATE VIEW

    @model SKUMAR.ViewModels.Invoice_Invoice_Details_ViewModel
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <hr />
        <div class="col-md-6">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.DATE, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.DATE, new { @Value = ViewBag.CURRENTDATE, @autofocus = "autofocus", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.DATE, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.INVOICENO, htmlAttributes: new { @autofocus = "autofocus", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.INVOICENO, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.INVOICENO, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.PARTY, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.PARTY, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.PARTY, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.BROKER, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.BROKER, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.BROKER, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.TERMS, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.TERMS, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.TERMS, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.BROKERAGE, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.BROKERAGE, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.BROKERAGE, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.ARTICLE, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.ARTICLE, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.ARTICLE, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Invoices.DETAILS, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Invoices.DETAILS, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Invoices.DETAILS, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <table class="table table-bordered">
                <tbody>
                    <tr>
                        <td>
                          //THIS IS WHERE I WANT TO ENTER MULTIPLE INVOICE_DETAILS INFORMATION AND THEN SEND TO CONTROLLER FOR SAVE
                        </td>
                    </tr>
                </tbody>
            </table>
        </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>
}


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

There will be single Invoice entry and multiple Invoice_Details entry

Now can you tell me how to generate view for this view model so that I can enter single detail about Invoice and multiple entries for Invoice_Details?

Also show me how can I send this information to controller and how to save them?

Hemal
  • 3,682
  • 1
  • 23
  • 54
  • 1
    [See if my answer here helps you](http://stackoverflow.com/questions/25286797/how-to-handle-repeating-form-fields-in-asp-mvc/25287349#25287349) – jamesSampica Jan 10 '17 at 16:14
  • 1
    View models, especially when editing should never contain properties which are data models. For dynamically adding collection items in a vew, refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options –  Jan 10 '17 at 20:56
  • I can not get any help from above links, can you show me any tutorial where from single view data is saved in two models, one is single and other is IENUMERABLE? – Hemal Jan 12 '17 at 12:11

0 Answers0