2

Users can take multiple courses and they can fill the course details in the text box by adding them on button click.

Had searched and got these two links but didn't understood how to use it in my situation.

Adding dynamic controls and get their values

Adding dynamic controls and get their values in MVC

I am using jquery to add text box on client side.

<script>
$(document).ready(function () {
    $("#txt").click(function () {
        var $ctrl = $('<input/>').attr({ type: 'text', name: 'text', value: '' }).addClass("text");
        $("#holder").append($ctrl);
    })
});    

The text boxes are added successfully on each button click like if a user wants 5 text box then they can do that,but how to get all the 5 text box values in a model so that I can save them in 5 rows in my table

<div style="display:inline">
<input type="button" id="txt" value="Add TextBox" style="" />
</div>
<div id="holder">
</div>

Model

public class tblCourse
{
    public int Id { get; set; }

    public string CourseName { get; set; }
}

Viewmodel

public class VMStudentDetails
{
    public List<tblStudents> students { get; set; }

    public tblStudentDetails StudentDetails { get; set; }

    public List<tblCourse> Course { get; set; }

}

Action

 [HttpPost]
    public ActionResult Save(VMStudentDetails model)
    {
        return View();
    }

View

@using (Html.BeginForm("Save", "Student", FormMethod.Post,
                                  new { enctype = "multipart/form-data" }))
 {
      <input type="submit" value="Upload" />
 }
Community
  • 1
  • 1
Dave
  • 263
  • 6
  • 23
  • Are you trying to dynamically add new `tblCourse` items and bind to property `Course` of `VMStudentDetails` –  Jun 12 '16 at 11:20
  • its actually a small scenario i have to add multiple line items in tblCourse. – Dave Jun 12 '16 at 11:31
  • Like tblStudent will contain student name and the courses will be saved in tblCourse table 1 student multiple courses. – Dave Jun 12 '16 at 11:32
  • Yes I know. But again, are you trying to dynamically add new `tblCourse` items and bind to property `Course` of `VMStudentDetails` –  Jun 12 '16 at 11:34
  • Yes trying to achieve that. – Dave Jun 12 '16 at 11:35
  • So a `Student` can create a new `Course`? That does not make sense - surely they need to select from existing Courses. And having both `List students` and `List Course` does not make sense either. What are you trying to do with this view? Is the idea that a student can see a list of all courses and select one or more of them? –  Jun 12 '16 at 11:39
  • This situation is just a small example and is not the actual requirement the actual requirement is a bit complex,goal is to save the dynamic text box values in the table. – Dave Jun 12 '16 at 11:42

0 Answers0