Invoice Class:
- public class Invoice
- {
- public Invoice()
- {
- }
- [Required]
- [Key]
- public int InvoiceID { get; set; }
- [Required]
- [StringLength(30)]
- [DisplayName("Invoice Number")]
- public string InvoiceNumber { get; set; }
- [Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
- [DataType(DataType.Date)]
- [Column(TypeName = "Date")]
- [DisplayName("Invoice Date")]
- public DateTime InvoiceDate { get; set; }
- public List
InvoiceLines { get; set; } - [ForeignKey("Client")]
- public int OwnerClientIDFK { get; set; }
- [DisplayName("Client")]
- public Client Client { get; set; }
- }
- public class InvoiceLine
- {
- public InvoiceLine()
- {
- }
- [Key]
- [Required]
- public int InvoiceLineId { get; set; }
- [Required]
- [StringLength(255)]
- [DisplayName("Item")]
- public string ItemName { get; set; }
- [DisplayName("Description")]
- public string ItemDescription { get; set; }
- [Required]
- public int Quantity { get; set; }
- [Required]
- [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
- public decimal Value { get; set; }
- [ForeignKey("ParentInvoice")]
- public int InvoiceID { get; set; }
- public Invoice ParentInvoice { get; set; }
- }
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- // Invoice invoice = db.Invoices.Find(id);
- Invoice invoice = db.Invoices.Include(i => i.InvoiceLines)
- .Include(i => i.Client)
- .Where(c => c.InvoiceID == id).FirstOrDefault();
- if (invoice == null)
- {
- return HttpNotFound();
- }
- ViewBag.OwnerClientIDFK = new SelectList(db.Clients, "ClientId", "CompanyName", invoice.OwnerClientIDFK);
- return View(invoice);
- }
- public ActionResult Edit([Bind(Include = "InvoiceID,InvoiceNumber,InvoiceDate,OwnerClientIDFK")] Invoice invoice)
- {
- if (ModelState.IsValid)
- {
- db.Entry(invoice).State = EntityState.Modified;
- foreach (var invLine in invoice.InvoiceLines)
- {
- db.Entry(invLine).State = EntityState.Modified;
- }
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- ViewBag.OwnerClientIDFK = new SelectList(db.Clients, "ClientId", "CompanyName", invoice.OwnerClientIDFK);
- return View(invoice);
- }
View:
- @model DemoApp.Entities.Invoice
- @{
- ViewBag.Title = "Edit";
- }
- <h2>Edith2>
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- <div class="form-horizontal">
- <h4>Invoiceh4>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- @Html.HiddenFor(model => model.InvoiceID)
- <div class="form-group">
- @Html.LabelFor(model => model.InvoiceNumber, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.InvoiceNumber, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.InvoiceNumber, "", new { @class = "text-danger" })
- div>
- div>
- <div class="form-group">
- @Html.LabelFor(model => model.InvoiceDate, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.InvoiceDate, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.InvoiceDate, "", new { @class = "text-danger" })
- div>
- div>
- <div class="form-group">
- @Html.LabelFor(model => model.OwnerClientIDFK, "OwnerClientIDFK", htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.DropDownList("OwnerClientIDFK", null, htmlAttributes: new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.OwnerClientIDFK, "", new { @class = "text-danger" })
- div>
- div>
- <div class="form-group">
- <h2>Invoice Linesh2>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="row">
- <div class="col-md-8">
- <table class="table">
- <thead>
- <tr>
- <th>Itemth>
- <th>Descriptionth>
- <th>Qtyth>
- <th>Unit Valueth>
- tr>
- thead>
- <tbody>
- @for (int i = 0; i < Model.InvoiceLines.Count; i++)
- {
- <tr>
- <td>@Html.EditorFor(x => x.InvoiceLines[i].ItemName, new { htmlAttributes = new { @class = "form-control" } })td>
- tr>
- }
- tbody>
- table>
- div>
- div>
- div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Save" class="btn btn-default" />
- div>
- div>
- div>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
Prateek SinglaPosted May 7, 2018, 7:12 AM
Darren BrookPosted May 4, 2018, 7:51 AM
Prateek SinglaPosted May 4, 2018, 12:14 AM
Darren BrookPosted May 1, 2018, 4:23 AM
Prateek SinglaPosted Apr 30, 2018, 11:33 PM