【发布时间】:2018-07-22 15:37:38
【问题描述】:
我正在使用 webgrid 在编辑模式下以表格格式显示数据。当我的应用程序运行时,文本框以表格格式显示数据。当用户更改文本框中的现有数据并单击提交按钮时,我的服务器端操作被调用但没有数据传递到那里,这导致我的 List<UserModel> oUserModel 始终为空
我在这里粘贴我的视图和操作代码。
查看代码
@model List<MVCCRUDPageList.Controllers.UserModel>
@using (Html.BeginForm(null,null,FormMethod.Post))
{
var grid = new WebGrid(Model);
var rowNum = 0;
<div id="gridContent" style=" padding:20px; ">
@grid.GetHtml(
tableStyle: "table",
alternatingRowStyle: "alternate",
selectedRowStyle: "selected",
headerStyle: "header",
columns: grid.Columns
(
//grid.Column("RowNumber", style: "HideFirstCol", format: item => rowNum = rowNum + 1),
grid.Column(null, null, format: @<input type="hidden" name="IDHidden" value="rowNum + 1" />),
grid.Column("First Name",
style: "col2",
format: @<text>
@Html.TextBox("UserModel[" + (rowNum - 1).ToString() + "].FirstName", (object)item.FirstName)
</text>),
grid.Column("Last Name",
style: "col2",
format: @<text>
@Html.TextBox("UserModel[" + (rowNum - 1).ToString() + "].LastName", (object)item.LastName)
</text>)
))
</div>
<input type="submit" name="SaveButton" value="Save" />
}
动作代码
public class WebGridEditableController : Controller
{
// GET: WebGridEditable
public ActionResult Index()
{
List<UserModel> users = UserModel.getUsers();
return View(users);
}
// GET: WebGridEditable
[HttpPost]
public ActionResult Index(List<UserModel> oUserModel)
{
return View(oUserModel);
}
}
public class UserModel
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public static List<UserModel> getUsers()
{
List<UserModel> users = new List<UserModel>()
{
new UserModel (){ ID=1, FirstName="Anubhav", LastName="Chaudhary" },
new UserModel (){ ID=2, FirstName="Mohit", LastName="Singh" },
new UserModel (){ ID=3, FirstName="Sonu", LastName="Garg" },
new UserModel (){ ID=4, FirstName="Shalini", LastName="Goel" },
new UserModel (){ ID=5, FirstName="James", LastName="Bond" },
};
return users;
}
}
请强调我在那里犯了哪些错误,导致表格数据无法传递到服务器端操作。
屏幕截图
【问题讨论】:
-
可以分享生成的html的截图等吗?
-
@BurakSARICA 屏幕截图已添加....如果可能,请查看。
-
@MonojitSarkar 我的意思是页面的 html 源代码。
标签: asp.net-mvc webgrid