Click here to Skip to main content
15,884,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

i have a student class like below

C#
public class Student
    {
        public int stdId { set; get; }
        public string stdName { get; set; }
        public string dept { get; set; }
    }


and my controller code is

C#
public ActionResult Student()
       {

           List<Student> lstStd = new List<Student>() { new Student()
           { stdId = 103, stdName = "bharath", dept = "CSE" },
           new Student() { stdId = 104, stdName = "Hari", dept = "IT" } };

           return View(lstStd);
       }

       [HttpPost]
       public ActionResult Student(IEnumerable<Student> lstStd)
       {
           return View();
       }


and View code is


C#
@using NewMVC.Models
@model IEnumerable<Student>
<h2>
    @using (Html.BeginForm())
    {
        foreach (var data in Model)
        {
        <table>
            <tr>
                <td>Student Name is
                </td>
                <td>@Html.TextBoxFor(p => data.stdId)</td>
            </tr>
            <tr>
                <td>Student Id is
                </td>
                <td>@Html.TextBoxFor(p => data.stdName)</td>
            </tr>
            <tr>
                <td>Student Dept is
                </td>
                <td>@Html.TextBoxFor(p => data.dept)</td>
            </tr>
        </table>
        }
        <input type="submit" value="create" name="hi" />
    }
</h2>


when i am trying to get the binded model data from the view,the List<student> is coming as null.

Can any one please help me in finding the solution.

Thanks in advance..
Posted

hello i hope this works for you.. used instead this
XML
List<Student> lstStd = new List<Student>() { new Student()
           { stdId = 103, stdName = "bharath", dept = "CSE" },
           new Student() { stdId = 104, stdName = "Hari", dept = "IT" } };

use
XML
List<Student> lstStd = new List<Student>() ;

lstStd.add(
{ new Student()
           { stdId = 103, stdName = "bharath", dept = "CSE" }});
lstStd.add(
           new Student() { stdId = 104, stdName = "Hari", dept = "IT" }} );


return View(lstStd);

if this not work then please give me comment what is happen...

ok
 
Share this answer
 
Hi
I made a small change in your source. Please check.

C#
public class StudentController : Controller
  {
      //
      // GET: /Student/

      public ActionResult Student()
      {
          StudentList stdlist = new StudentList();

          List<student> lstStd = new List<student>() { new Student()
          { stdId = 103, stdName = "bharath", dept = "CSE" },
          new Student() { stdId = 104, stdName = "Hari", dept = "IT" } };

          stdlist.students = lstStd;
          return View(stdlist);
      }

      [HttpPost]
      public ActionResult Student(StudentList stdlst)
      {
          return View();
      }

  }
  public class StudentList
  {
      public List<student> students { get; set; }
  }
  public class Student
  {
      public int stdId { set; get; }
      public string stdName { get; set; }
      public string dept { get; set; }
  }


View
C#
   @model MyMvcApp.Web.UI.Controllers.StudentList

<h2>
    @using (Html.BeginForm())
    {
       for(int i = 0; i < Model.students.Count(); i ++)
        {
        <table>
            <tr>
                <td>Student Name is
                </td>
                <td>@Html.TextBoxFor(p => Model.students[i].stdId)</td>
            </tr>
            <tr>
                <td>Student Id is
                </td>
                <td>@Html.TextBoxFor(p =>  Model.students[i].stdName)</td>
            </tr>
            <tr>
                <td>Student Dept is
                </td>
                <td>@Html.TextBoxFor(p =>  Model.students[i].dept)</td>
            </tr>
        </table>
        }
        <input type="submit" value="create" name="hi" />
    }
</h2>


Please check the following url, you may get some idea why it is not working with foreach.

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/[^]
 
Share this answer
 
v5
Comments
Aaditya parcha 2-Jan-14 2:02am    
Hi, Thanks for the reply. i tried the above suggested way.
1)But that is not working.Can u tell me exactly why do i need to Take another class(StudentList) as u explained
2)Thats way is working but not with foreach.when i used for instead of foreach its workign fine. can u tell me what is made difference between use of for and foreach Thanks in advance
Dominic Abraham 2-Jan-14 6:06am    
I improved my first solution (Solution 1). Please have a look at that.
use following syntax:
C#
@using (Html.BeginForm(Action name,controller name))

eg in your code
C#
@using (Html.BeginForm(student,student))


Add breakpoints to codebehind to check flow of the execution
 
Share this answer
 
Comments
Aaditya parcha 2-Jan-14 2:04am    
Hi,
Thanks for the reply.

i tried the above suggested way.
but thats not wokring.can u tell me what is this do

thanks in advance
Vinay Jade 2-Jan-14 8:34am    
add breakpoints and observe where it stops..
and i advise you,dont use foreach for insert and edit records cause we are adding or editing record one by one if you want to do bulk edit or insert i suggest you to use kendoui grid for kendoui grid visit following link for demo
http://demos.kendoui.com/web/grid/index.html
and go for batch editing option

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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