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
@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
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
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 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]
public ActionResult Myview(IEnumerable<MyTest.Models.StudentInfo> StuInfo)
{
List<StudentInfo> Newdata = StuInfo;
StudentInfo SS = new StudentInfo();
SS.ID=3;
SS.Name="Michael";
Newdata.Add(SS);
return PartialView("Myview", Newdata);
}
}
}