I have a DB with a master table called "facturas" and another detail table "facturas_detalle." I would like to insert to them, so this is a "Database-First".
I need some guidance or advise of best practice here. I've read some examples that are outdated versions of Entity Framework and ASP with no MVC approach.
**facturas** folio PK fecha almacen FK cliente FK plaza FK usuario FK id_factura ---------- **facturas_detalle** folio FK articulo FK cantidad precio
I already created a model (using the assistant) from the database and I have stored procedures to write to them. Using the Store Procedure Mapping from VS 2015 I mapped the entities to those store procedures for Insert. I didn't marked pluralize option because some other tables names are uppercase and aren't nouns.
I also generated a controller and views for the "facturas" table, and it creates successfully entries in DB. How should I edit the view and controller to Insert one or more facturas_detalle
in the same page? Is this approach correct?
facturas_pruebaController.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Abaco_ASP.Models;
namespace Abaco_ASP.Controllers
{
public class facturas_pruebaController : Controller
{
private PAQAEntities db = new PAQAEntities();
// GET: facturas_prueba/Create
public ActionResult Create()
{
ViewBag.almacen = new SelectList(db.CATALMA, "COD_ALM", "NOM_ALM");
ViewBag.cliente = new SelectList(db.CATCTES, "COD_CTE", "NOM_CTE");
ViewBag.usuario = new SelectList(db.FACPARU, "cod_usu", "cod_Alm");
ViewBag.plaza = new SelectList(db.PLAZAS, "PLAZA", "LAST_COD_CTE");
return View();
}
// POST: facturas_prueba/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "folio,fecha,almacen,cliente,plaza,usuario,id_factura")] facturas_prueba facturas_prueba)
{
if (ModelState.IsValid)
{
db.facturas_prueba.Add(facturas_prueba);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.almacen = new SelectList(db.CATALMA, "COD_ALM", "NOM_ALM", facturas_prueba.almacen);
ViewBag.cliente = new SelectList(db.CATCTES, "COD_CTE", "NOM_CTE", facturas_prueba.cliente);
ViewBag.usuario = new SelectList(db.FACPARU, "cod_usu", "cod_Alm", facturas_prueba.usuario);
ViewBag.plaza = new SelectList(db.PLAZAS, "PLAZA", "LAST_COD_CTE", facturas_prueba.plaza);
return View(facturas_prueba);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Create.cshtml:
@model Abaco_ASP.Models.facturas_prueba
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>facturas_prueba</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.folio, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.folio, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.folio, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.fecha, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fecha, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fecha, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.almacen, "almacen", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("almacen", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.almacen, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.cliente, "cliente", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("cliente", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.cliente, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.plaza, "plaza", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("plaza", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.plaza, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.usuario, "usuario", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("usuario", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.usuario, "", new { @class = "text-danger" })
</div>
</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>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}