6 Stimmen

MVC Radiobutton Bindung komplexes Objekt

Ich habe MVC3-Webanwendung, wo wir brauchen, um Radio-Button-Liste mit Validierung aufzufüllen. Mein Modell ist etwas wie dieses:

public class EmployeesViewModel
{
     public List<Employee> listEmployee { get; set; } //To persist during post
     public IEnumerable<SelectListItem> selectListEmployee { get; set; }

     [Required]
     public Employee selectedEmployee { get; set; }
}

public class Employee
{
  public int ID {get; set;}
  public string Name {get; set}
  public string Department {get; set}
 }

Ich muss radiobutton Liste etwas wie unten zu füllen:

  • Employee1ID - Employee1Name - Employee1Department // id - name - department
  • Mitarbeiter2ID - Mitarbeiter2Name - Mitarbeiter2Abteilung
  • Mitarbeiter3ID - Mitarbeiter3Name - Mitarbeiter3Abteilung

Der ausgewählte Mitarbeiter sollte im Feld "selectedEmployee" gespeichert werden. Was ist der beste oder saubere Weg, um diese Optionsschaltfläche Liste in MVC3 aufzufüllen?

Anmerkung: Gesucht werden vor allem zwei Aufgaben: 1. Speichern des "Employee"-Objekts in jedem "Input"-Radiobutton-Tag, so dass der ausgewählte Mitarbeiter im Feld "selectedEmployee" gespeichert wird 2. Beste Möglichkeit, das Objekt "Employee" als Pflichtfeld zu markieren

Vielen Dank für Ihre Hilfe!

Gracias,

12voto

Darin Dimitrov Punkte 990883

Ich würde Ihnen Folgendes empfehlen. Beginnen Sie mit einem sauberen Ansichtsmodell, das wirklich ausdrückt, was die Ansicht an Informationen enthält:

public class EmployeesViewModel
{
    public List<EmployeeViewModel> ListEmployee { get; set; }

    [Required]
    public int? SelectedEmployeeId { get; set; }
}

public class EmployeeViewModel
{
    public int ID { get; set; }
    public string Label { get; set; }
}

dann einen Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new EmployeesViewModel
        {
            ListEmployee = GetEmployees()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(EmployeesViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // the model is invalid, the user didn't select an employee
            // => refetch the employee list from the repository and
            // redisplay the view so that he can fix the errors
            model.ListEmployee = GetEmployees();
            return View(model);
        }

        // validation passed at this stage
        // TODO: model.SelectedEmployeeId will contain the id
        // of the selected employee => use your repository to fetch the
        // actual employee object and do something with it 
        // (like grant him the employee of the month prize :-))

        return Content("thanks for submitting", "text/plain");
    }

    // TODO: This doesn't belong here obviously
    // it's only for demonstration purposes. In the real 
    // application you have a repository, use DI, ...
    private List<EmployeeViewModel> GetEmployees()
    {
        return new[]
        {
            new EmployeeViewModel { ID = 1, Label = "John (HR)" },
            new EmployeeViewModel { ID = 2, Label = "Peter (IT)" },
            new EmployeeViewModel { ID = 3, Label = "Nathalie (Sales)" },
        }.ToList();
    }
}

und schließlich eine Aussicht:

@model EmployeesViewModel

@using (Html.BeginForm())
{
    @Html.ValidationMessageFor(x => x.SelectedEmployeeId)
    @foreach (var employee in Model.ListEmployee)
    {
        <div>
            @Html.RadioButtonFor(x => x.SelectedEmployeeId, employee.ID, new { id = "emp" + employee.ID })
            @Html.Label("emp" + employee.ID, employee.Label)
        </div>
    }
    <input type="submit" value="OK" />
}

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X