嗨,今天的问候!
(1) 绑定到视图的视图模型(MyViewModel.cs)如下...
public class MyViewModel
{
public int ParentId { get; set; } //property1
List<Item> ItemList {get; set;} //property2
public MyViewModel() //Constructor
{
ItemList=new List<Item>(); //creating an empty list of items
}
}
(2) 我正在通过 ajax 回发(来自 MyView.cshtml 视图)调用一个操作方法,如下所示。
function AddItem() {
var form = $('#MyForm');
var serializedform = form.serialize();
$.ajax({
type: 'POST',
url: '@Url.Content("~/MyArea/MyController/AddItem")',
cache: false,
data: serializedform,
success: function (html) {$('#MyForm').html(html);}
});
}
下面的按钮单击将调用 ajax 回发...
<input type="button" value="Add" class="Previousbtn" onclick="AddItem()" />
(3) 我在(MyController.cs 控制器)中有一个动作方法,如下所示......
public ActionResult AddItem(MyViewModel ViewModel)
{
ViewModel.ItemList.Add(new Item());
return View("MyView", ViewModel);
}
现在的问题是,从动作返回后,视图模型中没有数据。但我能够获得第三次回发的数据!你能建议解决方案吗..
视图中的完整表单代码如下...
@model MyViewModel
<script type="text/javascript" language="javascript">
function AddItem() {
var form = $('#MyForm');
var serializedform = form.serialize();
$.ajax({
type: 'POST',
url: '@Url.Content("~/MyArea/MyController/AddItem")',
cache: false,
data: serializedform,
success: function (html) {
$('#MyForm').html(html);
}
});
}
function RemoveItem() {
var form = $('#MyForm');
var serializedform = form.serialize();
$.ajax({
type: 'POST',
url: '@Url.Content("~/MyArea/MyController/RemoveItem")',
cache: false,
data: serializedform,
success: function (html) {
$('#MyForm').html(html);
}
});
}
function SaveItems() {
var form = $('#MyForm');
var serializedform = forModel.serialize();
$.ajax({
type: 'POST',
url: '@Url.Content("~/MyArea/MyController/SaveItems")',
cache: false,
data: serializedform,
success: function (html) {
$('#MyForm').html(html);
}
});
}
</script>
@using (Html.BeginForm("SaveItems", "MyController", FormMethod.Post, new { id = "MyForm" }))
{
@Html.HiddenFor(m => Model.ParentId)
<div>
<input type="button" value="Save" onclick="SaveItems()" />
</div>
<div>
<table>
<tr>
<td style="width: 48%;">
<div style="height: 500px; width: 100%; overflow: auto">
<table>
<thead>
<tr>
<th style="width: 80%;">
Item
</th>
<th style="width: 10%">
Select
</th>
</tr>
</thead>
@for (int i = 0; i < Model.ItemList.Count; i++)
{
@Html.HiddenFor(m => Model.ItemList[i].ItemId)
@Html.HiddenFor(m => Model.ItemList[i].ItemName)
<tr>
@if (Model.ItemList[i].ItemId > 0)
{
<td style="width: 80%; background-color:gray;">
@Html.DisplayFor(m => Model.ItemList[i].ItemName)
</td>
<td style="width: 10%; background-color:gray;">
<img src="@Url.Content("~/Images/tick.png")" alt="Added"/>
@Html.HiddenFor(m => Model.ItemList[i].IsSelected)
</td>
}
else
{
<td style="width: 80%;">
@Html.DisplayFor(m => Model.ItemList[i].ItemName)
</td>
<td style="width: 10%">
@if ((Model.ItemList[i].IsSelected != null) && (Model.ItemList[i].IsSelected != false))
{
<img src="@Url.Content("~/Images/tick.png")" alt="Added"/>
}
else
{
@Html.CheckBoxFor(m => Model.ItemList[i].IsSelected, new { @style = "cursor:pointer" })
}
</td>
}
</tr>
}
</table>
</div>
</td>
<td style="width: 4%; vertical-align: middle">
<input type="button" value="Add" onclick="AddItem()" />
<input type="button" value="Remove" onclick="RemoveItem()" />
</td>
</tr>
</table>
</div>
}