I'm currently working on a new Web App using .net core MVC and I'm currently in need of a dropdownlist to select an element. (I wanted to show the name and save the id of it) The View (Edit.cshtml)
@model AdminLTE.Models.Edits1
@{
ViewBag.Title = "Edit";
}
<h2>Editar</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Edits</h4>
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id2)
<div class="form-group">
@Html.LabelFor(model => model.Id1, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Id1)
@Html.ValidationMessageFor(model => model.Id1)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Codigo, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Codigo)
@Html.ValidationMessageFor(model => model.Codigo)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Nome, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nome)
@Html.ValidationMessageFor(model => model.Nome)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Guardar" class="btn btn-primary" />
<a class="btn btn-default" asp-controller="Edit" asp-action="Index">Voltar</a>
</div>
</div>
</div>
}
The controller (Edit section)
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Edits edits = _context.Edits.Find(id);
ViewBag.Othermodel = GetOthermodel();
if (edits == null)
{
return HttpNotFound();
}
return View(edits);
}
private ActionResult HttpNotFound()
{
throw new HttpException(404, "Page Not Found");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind("Id2,Id1,Codigo,Nome")] Edits edits)
{
if (ModelState.IsValid)
{
_context.Entry(edits).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(edits);
}
private List<Othermodel> GetOthermodel()
{
List<Othermodel> othermodel = _context.Othermodel.ToList();
return othermodel;
}
private List<Edits> GetEdits()
{
List<Edits> edits = _context.Edits.ToList();
return edits;
}
How do I get the edit form to get data from the right controller (Edits) instead of (Edits1) and how do i get a dropdownlist for Id1 (showing a name instead of the id) (Some changes to names of vars)