2
Need to edit multiple records simultaneously, more specifically a list of students and load/edit from several textbox
linked to such.
Basically I need to give/take their presence.
How could you proceed?
2
Need to edit multiple records simultaneously, more specifically a list of students and load/edit from several textbox
linked to such.
Basically I need to give/take their presence.
How could you proceed?
3
If the student list is in the template you pass to the view you can use a EditorFor
... I don’t know what your model looks like, so I’m going to assume a structure like below:
@for (var i = 0; i < Model.Alunos.Count; i++)
{
<div class="field">
@Html.EditorFor(m => m.Alunos[i].Nome)
</div>
}
EDIT
Assuming a model class called Aluno
:
public class Aluno
{
public int Id { get; set; }
public string Nome { get; set; }
}
You can create a controller with actions called EditMany
for GET and POST:
public ActionResult EditMany()
{
var alunos = db.Alunos.ToArray();
return View(alunos);
}
[HttpPost]
public ActionResult EditMany(Aluno[] alunos)
{
if (!ModelState.IsValid)
return View(alunos);
try
{
// TODO: editar cada um dos alunos no banco de dados
// supondo que esteja usando o entity framework:
foreach (var aluno in alunos)
db.Entry(aluno).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View(alunos);
}
}
And a view called EditMany.cshtml
, who receives a list of Students:
@model IList<Mvc3Application.Models.Aluno>
@{
ViewBag.Title = "Edit Many";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Alunos</legend>
@for (var i = 0; i < this.Model.Count; i++)
{
@Html.HiddenFor(model => model[i].Id)
<div class="editor-label">
@Html.LabelFor(model => model[i].Nome)
</div>
<div class="editor-field">
@Html.EditorFor(model => model[i].Nome)
@Html.ValidationMessageFor(model => model[i].Nome)
</div>
}
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
And how do I recover this data in Controller? My biggest problem is handling data reception.
Just receive in the controller action the same type passed to the view.
I’ll make an example here and already put.
There’s the example friend... I hope it helps you.
Thank you! It worked.
Browser other questions tagged c# asp.net-mvc textbox
You are not signed in. Login or sign up in order to post.
I think you should break your question into several questions, because there are several steps involved in your question in question, it is difficult to know exactly where your question is. See example the answer below.
– Math