我是asp.net MVC的新手。我的项目中有一张动态表格。在表中添加动态行与下面的链接在Asp.net mvc4剃刀视图中编辑和更新动态表中的行
Adding and deleting rows in dynamic table in Asp.net mvc razor view
我需要编辑和更新动态表的帮助下实现的。 我曾尝试下面的代码
我的样品模型
public class Gift
{
public string Name { get; set; }
public double Price { get; set; }
}
public class GiftViewModel
{
public string Age { get; set; }
public DateTime TheDate { get; set; }
public IEnumerable<Gift> Gifts { get; set; }
}
我的样品控制器
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(GiftViewModel model)
{
// do work here
return RedirectToAction("Index");
}
public ViewResult AddNew()
{
return View("_TimeSheetView");
}
}
我的样品管窥
@model HelloWorldMvcApp.Gift
@using (Html.BeginCollectionItem("giftList"))
{
<div>
<span class="drop_medium">
@Html.TextBoxFor(m => m.Name)
</span>
<span class = "drop_medium">
@Html.TextBoxFor(m => m.Price)
</span>
</div>
}
我的样本主视图
@model HelloWorldMvcApp.GiftViewModel
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Age)
@foreach (var data in Model.Gifts)
{
{ Html.RenderPartial("_TimeSheetView", data); }
}
@Html.ActionLink("Add another", "AddNew", null, new { id="addItem" })
<input type="submit" value="Save"/>
}
<script type="text/javascript">
$("#addItem").click(function() {
$.ajax({
url: this.href,
cache: false,
success: function (html) { $("#dynamic").append(html); }
});
return false;
});
</script>
当我点击'添加另一个'按钮时,一行被添加到表中。在编辑表格中的值之后当我单击提交按钮时,我在控制器中没有收到任何内容。 IEnumerable Gifts变量为null。如何将表格值传递给控制器。请帮我解决这个问题。在此先感谢
你的模型属性被命名为''礼品所以它的需求是'Html.Begin CollectionItem(“Gifts”))'(不是'“giftlist”')。而且您还没有显示添加新项目的脚本。 –
@StephenMuecke添加新行没有问题。我会改变你提到的代码,并让你知道 – mvm
@StephenMuecke我像你所说的那样将“礼物列表”更改为“礼物”,但它没有奏效。我收到礼物为空 – mvm