Asp.Net MVC Post List of Objects

ASP.NET MVC allows you to easily post a list of objects from a view to a controller. This can be useful when you need to submit multiple items in a single request. Below is a step-by-step guide with code examples to help you achieve this in a clean and organized manner.

1. Model Definition

Create a Person class to represent the objects you want to post.

// Models/Person.cs
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

2. Controller Setup

Create a controller with two actions: one to render the initial view and another to handle the form submission.

// Controllers/YourController.cs
using System.Collections.Generic;
using System.Web.Mvc;

public class YourController : Controller
{
    public ActionResult Index()
    {
        List<Person> peopleList = GetPeopleList(); // Replace with your data retrieval logic
        return View(peopleList);
    }

    [HttpPost]
    public ActionResult ProcessPeople(List<Person> people)
    {
        if (ModelState.IsValid)
        {
            // Validations passed, process the list of people
            foreach (var person in people)
            {
                // Save to the database or perform other actions
            }

            return RedirectToAction("Success"); // Redirect to a success page
        }

        // ModelState is not valid, return to the form with errors
        return View("Index", people);
    }

    private List<Person> GetPeopleList()
    {
        // Replace this with your actual data retrieval logic
        List<Person> peopleList = new List<Person>
        {
            new Person { Id = 1, Name = "John Doe", Age = 25 },
            new Person { Id = 2, Name = "Jane Smith", Age = 30 },
            // Add more persons as needed
        };

        return peopleList;
    }
}

3. View Setup

Create a view (Index.cshtml) to render the form.

<!-- Views/Your/Index.cshtml -->
@model List<Person>

@using (Html.BeginForm("ProcessPeople", "Your", FormMethod.Post))
{
    for (int i = 0; i < Model.Count; i++)
    {
        @Html.HiddenFor(model => model[i].Id)
        <div>
            <label>Name:</label>
            @Html.TextBoxFor(model => model[i].Name)
        </div>
        <div>
            <label>Age:</label>
            @Html.TextBoxFor(model => model[i].Age)
        </div>
        <br />
    }

    <input type="submit" value="Submit" />
}

4. Submit the Form

Fill in the details and submit the form. The data should be posted to the ProcessPeople action, where you can process the list as needed.

This step-by-step tutorial should guide you through the process of posting a list of objects in an ASP.NET MVC application. Please adapt the code as per your specific requirements and replace the placeholder logic with your actual data retrieval or processing logic.

 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice