Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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:

C#
AddStudent addstudent = new AddStudent();
AddStudentList asl = new AddStudentList();
addStudent = asl.AddStudentData;
TryUpdateModel(addStudent);


This is my view for these models:
HTML
@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.
Posted
Updated 16-Apr-16 23:36pm
v2
Comments
Passion4Code 17-Apr-16 4:34am    
Are you using ajax call to pass model and update?
Shahzad Mirza 17-Apr-16 4:56am    
No I am using simple razor form to pass data to the model.
Passion4Code 17-Apr-16 5:23am    
Please check the answer provided below, if that helps, else post your issue back. :)

Try using the editor template for the model.
If possible please add the cshtml view code onto your question.
Please follow the below link
c# - Complex models and partial views - model binding issue in ASP.NET MVC 3[^]

Thanks
 
Share this answer
 
The problem is in controller to get data from the model and here is the code which I have used and it worked. I just use the parent model AddStudentList and pass the values to the database using AddStudentList.AddStudent and it worked fine.

Previous Code:
C#
AddStudent addstudent = new AddStudent();
AddStudentList asl = new AddStudentList();
addStudent = asl.AddStudentData;
TryUpdateModel(addStudent);


Replaced with this code:
C#
AddStudentList asl = new AddStudentList();
TryUpdateModel(asl);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900