Missing set a primary key in your Model, and a few more things. Change to the following:
public class Combustivel
{
[Key]
public int CombustivelId { get; set; }
[Required]
public decimal km_inicial { get; set; }
[Required]
public decimal km_final { get; set; }
[Required]
public decimal litros { get; set; }
[Required]
[DataType(DataType.Currency)]
public decimal valor { get; set; }
}
For Scaffolding, we may use the following clichés suggested by Microsoft for creation, editing and deletion:
public class CombustiveisController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Combustiveis
public async Task<ActionResult> Indice()
{
return View(await db.Combustiveis.ToListAsync());
}
// GET: Combustiveis/Detalhes/5
public async Task<ActionResult> Detalhes(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Combustivel combustivel = await db.Combustiveis.FindAsync(id);
if (combustivel == null)
{
return HttpNotFound();
}
return View(combustivel);
}
// GET: Combustiveis/Criar
public ActionResult Criar()
{
return View();
}
// POST: Combustiveis/Criar
// 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 async Task<ActionResult> Criar([Bind(Include = "CombustivelId,km_inicial,km_final,litros,valor")] Combustivel combustivel)
{
if (ModelState.IsValid)
{
db.Combustiveis.Add(combustivel);
await db.SaveChangesAsync();
return RedirectToAction("Indice");
}
return View(combustivel);
}
// GET: Combustiveis/Editar/5
public async Task<ActionResult> Editar(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Combustivel combustivel = await db.Combustiveis.FindAsync(id);
if (combustivel == null)
{
return HttpNotFound();
}
return View(combustivel);
}
// POST: Combustiveis/Editar/{id}
// 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 async Task<ActionResult> Editar([Bind(Include = "CombustivelId,km_inicial,km_final,litros,valor")] Combustivel combustivel)
{
if (ModelState.IsValid)
{
db.Entry(combustivel).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Indice");
}
return View(combustivel);
}
// GET: Combustiveis/Excluir/{id}
public async Task<ActionResult> Excluir(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Combustivel combustivel = await db.Combustiveis.FindAsync(id);
if (combustivel == null)
{
return HttpNotFound();
}
return View(combustivel);
}
// POST: Combustiveis/Excluir/{id}
[HttpPost, ActionName("Excluir")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ConfirmarExclusao(int id)
{
Combustivel combustivel = await db.Combustivels.FindAsync(id);
db.Combustivels.Remove(combustivel);
await db.SaveChangesAsync();
return RedirectToAction("Indice");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
Here I am considering that you are using Entity Framework with Microsoft SQL Server, as your question says.
The reply from @Randrade maps the DbSet
manually. The procedure of Scaffolding adds the DbSet
automatically for you to the context.
DbSet
? Context? Questions about how to use? See this answer.
As Views are also clichés:
Create.cshtml
@model OneeWeb_v2.Models.Combustivel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Combustivel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.km_inicial, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.km_inicial, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.km_inicial, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.km_final, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.km_final, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.km_final, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.litros, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.litros, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.litros, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.valor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.valor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.valor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Criar" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Voltar para Listagem", "Indice")
</div>
Edit.cshtml
@model OneeWeb_v2.Models.Combustivel
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Combustivel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CombustivelId)
<div class="form-group">
@Html.LabelFor(model => model.km_inicial, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.km_inicial, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.km_inicial, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.km_final, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.km_final, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.km_final, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.litros, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.litros, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.litros, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.valor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.valor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.valor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Salvar" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Voltar para Listagem", "Indice")
</div>
Delete.cshtml
@model OneeWeb_v2.Models.Combustivel
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Você tem certeza de que deseja excluir este registro?</h3>
<div>
<h4>Combustivel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.km_inicial)
</dt>
<dd>
@Html.DisplayFor(model => model.km_inicial)
</dd>
<dt>
@Html.DisplayNameFor(model => model.km_final)
</dt>
<dd>
@Html.DisplayFor(model => model.km_final)
</dd>
<dt>
@Html.DisplayNameFor(model => model.litros)
</dt>
<dd>
@Html.DisplayFor(model => model.litros)
</dd>
<dt>
@Html.DisplayNameFor(model => model.valor)
</dt>
<dd>
@Html.DisplayFor(model => model.valor)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Excluir" class="btn btn-default" /> |
@Html.ActionLink("Voltar para Listagem", "Indice")
</div>
}
</div>
Details.cshtml
@model OneeWeb_v2.Models.Combustivel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Combustivel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.km_inicial)
</dt>
<dd>
@Html.DisplayFor(model => model.km_inicial)
</dd>
<dt>
@Html.DisplayNameFor(model => model.km_final)
</dt>
<dd>
@Html.DisplayFor(model => model.km_final)
</dd>
<dt>
@Html.DisplayNameFor(model => model.litros)
</dt>
<dd>
@Html.DisplayFor(model => model.litros)
</dd>
<dt>
@Html.DisplayNameFor(model => model.valor)
</dt>
<dd>
@Html.DisplayFor(model => model.valor)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Editar", "Editar", new { id = Model.CombustivelId }) |
@Html.ActionLink("Voltar para Listagem", "Indice")
</p>
Are you using the Entity framework? If so, you have already set up the Dbcontext?
– Randrade
Yes I am using, and the Dbcontext configuration is in
IdentityModels
? if yes already!– Thomas Erich Pimentel
Pole also the
IdentityModels
– Randrade
as requested, edition made.
– Thomas Erich Pimentel
@Thomaserichpimentel You never used Entityframework?
– Jéf Bueno
No, I’m adventuring now. rs
– Thomas Erich Pimentel