Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to send HTML table from view to controller. I am beginner in MVC. i have table in view where i am displaying my data at the View. now i have to send this data in HTML file to controller when button Click but my HttpPost method seems not getting data. Please help me correct.

What I have tried:

MyView.cshtml
HTML
@using MyTest.Models

@model IEnumerable<StudentInfo>
 
@{
    ViewBag.Title = "MyView";
}
 
<h2>MyView</h2>
 
    <div style="background-color:blue; height:500px; id="aa">
        @using (Html.BeginForm("AddNew", "Home"))
        {
            <input type="submit" value="submit" id="btnsubmit" />
            <table style="width:100%;">
                <tr>
                    <th>ID.</th>
                    <th>Name</th>
                </tr>
                @if (Model != null )
                {
                foreach (StudentInfo SInfo in Model)
                {
                    <tr>
                        <td style="width:5%">@SInfo.ID</td>
                        <td style="width: 82%">@SInfo.Name</td>
                        <td style="width:5%"><input type="button" value="Delete" /></td>
                    </tr>
                    <tr style="border:solid; height:0%;" />
                }
                }
            </table>
        }
    </div>
-----------------------------------------------------------
Model--> StudentInfo.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace MyTest.Models
{
    public class BookInfo
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}


---------------------------------------------
Controller--> HomeController.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyTest.Models;

 
namespace MyTest.Controllers
{
 
    public class HomeController : Controller
    {
        
        public ActionResult Myview()
        {
            return View("Myview");
        }
        
        public ActionResult clickevent(string btnsave)
        {
            //return RedirectToAction("Index");
            //return this.SelectItem();
            return this.secondpg();
        }
        
        public ActionResult Myview()
        {
            List<StudentInfo> StuInfo = new <StudentInfo>();
            StudentInfo SInfo = new StudentInfo();
            
            SInfo.ID=1;
            SInfo.Name="Christ";
            StuInfo.Add(SInfo);
            
            SInfo = new StudentInfo();
            SInfo.ID=2;
            SInfo.Name="Steven";
            StuInfo.Add(SInfo);

         
 
            return View("MyView", StuInfo);
        }
 
        [HttpPost] // Why StuInfo Result is Null Value? I can't get the data back from View Table..
        public ActionResult Myview(IEnumerable<MyTest.Models.StudentInfo> StuInfo)
        {
            // The StudentInfo result is always... Why?

            List<StudentInfo> Newdata = StuInfo;
            
             // Additional record added.
             StudentInfo SS = new StudentInfo();
             SS.ID=3;
             SS.Name="Michael";
             Newdata.Add(SS);
            
            return PartialView("Myview", Newdata);
        }
 
    }
}
Posted
v3

You need to have some controls to post them. So, add controls inside the loop.

Refer - asp.net mvc - Pass Table Value from View to Controller MVC - Stack Overflow[^]
 
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