以下是您可以用来实现此目的的一种技术。
让我们先从视图模型:
public class ViewModel
{
public DateTime SomeDate { get; set; }
public string SomeString { get; set; }
public List<E> SomeList { get; set; }
}
public class E
{
public bool Selected { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }
}
然后我们写一些控制器来处理视图的渲染和AJAX请求:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new ViewModel
{
SomeDate = DateTime.Now,
SomeString = "some text",
SomeList = Enumerable.Range(1, 7).Select(x => new E
{
Foo = "foo " + x,
Bar = "bar " + x
}).ToList()
};
return View(model);
}
[HttpPost]
public ActionResult Index(ViewModel model)
{
// Here we will get our view model properly bound and
// the list will contain only the items that the user
// has selected (see below...)
// TODO: do some processing
return Content("Thanks for submitting this data", "text/plain");
}
}
然后我们转移到~/Views/Home/Index.cshtml
视图:
@model ViewModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.SomeDate)
@Html.EditorFor(x => x.SomeDate)
</div>
<div>
@Html.LabelFor(x => x.SomeString)
@Html.EditorFor(x => x.SomeString)
</div>
<table>
<thead>
<tr>
<th></th>
<th>Foo</th>
<th>Bar</th>
</tr>
</thead>
<tbody>
@Html.EditorFor(x => x.SomeList)
</tbody>
</table>
<input type="submit" value="Send selected values to server using AJAX" />
}
最后我们定义了一个的编辑器模板型(~/Views/Home/EditorTemplates/E.cshtml
)将被渲染为集合中的每个元素:
@{
var index = Guid.NewGuid().ToString();
var prefix = Regex.Replace(ViewData.TemplateInfo.HtmlFieldPrefix, @"\[\d+\]$", match =>
{
return string.Format("[{0}]", index);
});
ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}
<input type="hidden" name="SomeList.Index" value="@index" />
<tr>
<td>
@Html.DisplayFor(x => x.Foo)
@Html.HiddenFor(x => x.Foo)
</td>
<td>
@Html.DisplayFor(x => x.Bar)
@Html.HiddenFor(x => x.Bar)
</td>
<td>
@Html.CheckBoxFor(x => x.Selected)
</td>
</tr>
OK,所以在这个阶段,我们还没有编写的JavaScript一部分,所以这应该表现为一个普通的HTML表单,当它被提交时,它会将所有的值发送到服务器。
最后一部分是对表单进行AJAXify并仅POST用户在请求中选择的记录。因此,我们可以在一个单独的JavaScript文件做到这一点:
$(function() {
$('form').submit(function() {
// we clone the original form and we will
// filter out the non-selected fields
var myForm = $(this).clone(false, false);
$('tr', myForm).each(function() {
var isSelected = $(':checkbox', this).is(':checked');
if (!isSelected) {
$(this).remove();
}
});
$.ajax({
url: this.action,
type: this.method,
data: myForm.serialize(),
success: function (result) {
alert(result);
}
});
return false;
});
});
是好文章来处理动态列表,我会建议你following blog post。
感谢您的回复......我正在尝试您的代码,但显然当我序列化表单以使ajax调用List在服务器端代码中出现空值时...所有其他值都是正确的,但是列表显示为空。任何想法可能是什么问题? – AJC
@AJC,这很奇怪,因为这个代码在我的机器上测试时工作。 –
我检查了代码,它确实从列表中删除了所有未检查的项目,看起来好像列表没有被序列化。我尝试了没有AJAX只是为了看到问题仍然存在,列表在服务器中出现空。 – AJC