MVC How to pass a list of objects using a POST list item

I want to publish a list of controller elements using Razor, but I get the list of objects as null My cool structure

Model:

List<Subjects> modelItem

class Subjects
{
    int SubId{get;set;}
    string Name{get;set;}
    List<Students> StudentEntires{get;set;}
}

class StudentEntires
{
    int StudId{get;set;}
    string Name{get;set;}
    int Mark{get;set;}
}

The model itself is a list of elements, and each element contains a list of child elements. A sample model is a list of objects, and each item contains a list of students, and I want to enter a label for each student

My look is like

@model IList<Subjects>  
@{   
   Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{    
   @Html.ValidationSummary(true)
   if (Model.Count > 0)
   {
       @for (int item = 0; item < Model.Count(); item++)
       {
           <b>@Model[item].Name</b><br />
           @foreach (StudentEntires markItem in Model[item].StudentEntires)
           {
               @Html.TextBoxFor(modelItem => markItem.Mark)
           }
       }
       <p style="text-align:center">
           <input type="submit" class="btn btn-primary" value="Update" />
       </p>
    }
}

And in the controller

    [HttpPost]
    public ActionResult OptionalMarks(int Id,ICollection<Subjects> model)
    {
        //BUt my model is null. Any idea about this?
    }
+4
source share
2 answers

You find this difficult because you are not using the full power of the MVC environment, so let me introduce a working example.

, :

public class SubjectGradesViewModel
{
    public SubjectGradesViewModel()
    {
        Subjects = new List<Subject>();
    }

    public List<Subject> Subjects { get; set; }
}

:

public class Subject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Student> StudentEntries { get; set; }
}

, :

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Grade { get; set; }
}

, . , , , :

public ActionResult Index()
{
    var model = new SubjectGradesViewModel();

    // This sample data would normally be fetched
    // from your database
    var compsci = new Subject
    {
        Id = 1,
        Name = "Computer Science",
        StudentEntries = new List<Student>()
        {
            new Student { Id = 1, Name = "CompSci 1" },
            new Student { Id = 2, Name = "CompSci 2" },
        }
    };

    var maths = new Subject
    {
        Id = 2,
        Name = "Mathematics",
        StudentEntries = new List<Student>()
        {
            new Student { Id = 3, Name = "Maths 1" },
            new Student { Id = 4, Name = "Maths 2" },
        }
    };

    model.Subjects.Add(compsci);
    model.Subjects.Add(maths);

    return View(model);
}

[HttpPost]
public ActionResult Index(SubjectGradesViewModel model)
{
    if (ModelState.IsValid)
    {
        return RedirectToAction("Success");
    }

    // There were validation errors
    // so redisplay the form
    return View(model);
}

, , . Index:

@model SubjectGradesViewModel

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    @Html.EditorFor(m => m.Subjects) <br />
    <input type="submit" />
}

, Html.EditorFor, Subjects. , , , EditorTemplate Subject. . , EditorTemplates DisplayTemplates MVC, .

: Subject Student. :

  • EditorTemplates (, Home\Index.cshtml, Home\EditorTemplates).
  • , (.. , Subject.cshtml Student.cshtml ( , )).

Subject.cshtml :

@model Subject

<b>@Model.Name</b><br />

@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.Name)
@Html.EditorFor(m => m.StudentEntries)

Student.cshtml :

@model Student

@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.Name)
@Html.DisplayFor(m => m.Name): @Html.EditorFor(m => m.Grade)
<br />

. , POST, , .

, EditorTemplates , DisplayTemplates? , .

- , Html.EditorFor Html.DisplayFor, , , . , , . null Count(), . , .

EditorTemplates , POST . , , . , , .

+20

public ActionResult OptionalMarks(ICollection<Subjects> model)

HTML , - Id. .

foor

@for(int idx = 0; idx < Model[item].StudentEntires.Count();idx++)
{
    @Html.TextBoxFor(_ => Model[item].StudentEntries[idx])
}

, - foreach StudentEntries, , , , NULL.

EDIT:

:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var viewModel = new IndexViewModel();

        var subjects = new List<Subject>();
        var subject1 = new Subject();

        subject1.Name = "History";
        subject1.StudentEntires.Add(new Student { Mark = 50 });
        subjects.Add(subject1);

        viewModel.Subjects = subjects;

        return View(viewModel);
    }

    [HttpPost]
    public ActionResult Index(IndexViewModel viewModel)
    {
        return new EmptyResult();
    }
}

@model SOWorkbench.Controllers.IndexViewModel

@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    if (Model.Subjects.Any())
    {
        int subjectsCount = Model.Subjects.Count();
        for (int item = 0; item < subjectsCount; item++)
        {
            <b>@Model.Subjects[item].Name</b><br />            
            int studentEntriesCount = Model.Subjects[item].StudentEntires.Count();

            for(int idx = 0;idx < studentEntriesCount;idx++)
            {
                @Html.TextBoxFor(_ => Model.Subjects[item].StudentEntires[idx].Mark);
            }
        }
        <p style="text-align:center">
            <input type="submit" class="btn btn-primary" value="Update" />
        </p>
    }
}

, , viewModel.

+1

All Articles