如何将HTML表数据从视图传递到控制器MVC [英] How to pass HTML table data from view to controller MVC
问题描述
如何将HTML表从视图发送到控制器。我是MVC的初学者。我有视图中的表格,我在视图中显示我的数据。现在我必须在按钮单击时将这些数据以HTML文件形式发送给控制器,但我的HttpPost方法似乎没有获取数据。请帮我纠正。
我尝试了什么:
MyView .cshtml
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>
------------------- ----------------------------------------
型号 - - > StudentInfo.cs
-----------------------------------------------------------
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
---------------------------------------------
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 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);
}
}
}
推荐答案
你需要有一些控件来张贴他们。所以,在循环中添加控件。
参考 - asp.net mvc - 将表值从视图传递到控制器MVC - 堆栈溢出 [ ^ ]
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[^]
将动态添加的HTML表记录列表传递给ASP.NET MVC中的控制器 [ ^ ]
这篇关于如何将HTML表数据从视图传递到控制器MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!