I am trying to add data and list data into a single view using given models the data listing is working fine but while adding data no models is working.
I have two models AddStudent and ListStudent and I am using both of them in a view by merging into another model AddStudentList.
public class AddStudent
{
public int EnrollmentNo { get; set; }
public string StudentName { get; set; }
}
public class ListStudent
{
public int EnrollmentNo { get; set; }
public string StudentName { get; set; }
}
public class AddStudentList
{
public AddStudent addStudentData {get; set;}
public List<ListStudent> listStudentData {get; set;}
}
The code in StudentController that i am using:
AddStudent addstudent = new AddStudent();
AddStudentList asl = new AddStudentList();
addStudent = asl.AddStudentData;
TryUpdateModel(addStudent);
This is my view for these models:
@model AddStudentList
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Add Student</h1>
<hr>
<div class="row row-centered">
<div class="col-md-4 col-md-offset-3">
@using (Html.BeginForm("AddStudent", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.TextBoxFor(m => m.AddStudentData.EnrollmentNo, new { @class = "form-control animated", placeholder = "Category Name", tabindex = 1 })
@Html.ValidationMessageFor(m => m.AddStudentData.EnrollmentNo, null, new { @class = "error-class animated", @Style = "color:red;" })
</div>
<div class="form-group">
@Html.DropDownListFor(m => m.AddStudentData.StudentName, new { @class = "form-control animated", tabindex = 2 })
@Html.ValidationMessageFor(m => m.AddStudentData.StudentName, null, new { @class = "error-class animated", @Style = "color:red;" })
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="CategorySubmit" id="CategorySubmit" tabindex="6" class="form-control btn btn-login" value="Add Category">
</div>
</div>
</div>
}
</div>
</div>
@{
var grid = new WebGrid(Model.ListStudentData, canPage: true, rowsPerPage: 50);
}
@if (grid.Rows.Count > 0)
{
@grid.GetHtml(
htmlAttributes: new { @class = "table table-bordered table-responsive" },
columns:
grid.Columns(
grid.Column("EnrollmentNo", "Enrollment No."),
grid.Column("StudentName", "Student Name")))
}
What I have tried:
I have tried using individually child and parent model but still data is not passing from view to the required model.