Receiving several TextBox values

2

I need to simultaneously edit multiple records, more specifically a list of students, and load / edit from several% s of% s linked to it.

Basically I need to give them their presence.

How could I proceed?

    
asked by anonymous 09.05.2014 / 14:37

1 answer

3

If the list of students is in the model you pass to the view you can use EditorFor ... I do not know how your model is, so I'll 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 template class named 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 , which 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>
}
    
09.05.2014 / 14:43