ASP MVC - Get data from partial view on Create

Im using ASP.Net MVC 5. I have two simple classes; Student and course, for example:

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

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

I want to create a new one Coursewith an extra number Students. The form / view of the student (s) will be displayed as an insde the Course-view.

I currently have a Create-View that is strictly consistent Course. This view has only 1 text field - name Course.

I am making a partial view that is strongly typed for Student. To simplify, I just want to add 1 student to the List.

I would like to transfer student data to the course object, and then “go” to the controller.

- , MVC?;)

+4
2

, . -, Html- BeginCollectionItem. , / , .

( ).

Course. Ctor of course -.

RenderPartial partailview + .

@foreach(var student in Model.Students)
{
  RenderPartial("_Student", student);
}

:

@model Project.Data.Entities.Student

<div class="AddStudent form-group">
    @using (Html.BeginCollectionItem("students"))
    {

    @Html.Label("Name:", new { @class = "control-label col-md-2" })
    <div class="col-md-8">
        @Html.TextBoxFor(x => x.Name)

        <button type="button" class="deletButton btn btn-default">Remove</button>
        @Html.ValidationMessageFor(model => model.Name)
    </div>
    }
</div>

, , (jquery) .

, ajax "_Student".

<div>
            @Ajax.ActionLink("Add more...", "NewStudentRow", "Course", new AjaxOptions()
            {
                InsertionMode = InsertionMode.InsertAfter,
                UpdateTargetId = "students"
            }, new { @class = "btn btn-default" })
        </div>

NewStudentRow :

public PartialViewResult NewStudentRow ()
{
    return PartialView("_Student", new Student());
}

, http://www.nuget.org/packages/BeginCollectionItem/

+2

, .

-:

CourseView:

<TextBox>@Model.Name</TextBox>
@foreach(var student in Model.Students)
{
  RenderPartial("ShowStudent");
}
RenderPartial("AddStudent");

AddStudentView , . , (- RedirectToAction ( "Course", new {id = student.CourseId}) "". , .

ajax, , - , , .

0

All Articles