做网站需要什么条件泉州关键词快速排名
首先,创建一个视图模型来表示要编辑的内容。我假设cashAmount是一个货币值,因此应该是十进制(根据需要添加其他验证和显示属性)。public class CashRecipientVM{
public int? ID { get; set; }
public decimal Amount { get; set; }
[Required(ErrorMessage = "Please enter the name of the recipient")]
public string Recipient { get; set; } }
然后创建一个部分视图(例如)_Recipient.cshtml@model CashRecipientVM
@using (Html.BeginCollectionItem("recipients"))
{
@Html.HiddenFor(m => m.ID, new { @class="id" })
@Html.LabelFor(m => m.Recipient)
@Html.TextBoxFor(m => m.Recipient)
@Html.ValidationMesssageFor(m => m.Recipient)
@Html.LabelFor(m => m.Amount)
@Html.TextBoxFor(m => m.Amount)
@Html.ValidationMesssageFor(m => m.Amount)
Delete
}
以及返回该部分的方法public PartialViewResult Recipient(){
return PartialView("_Recipient", new CashRecipientVM());}
那么您的主要get方法将是public ActionResult Create(){
List model = new List();
.... // add any existing objects that your editing
return View(model);}
它的观点是@model IEnumerable@using (Html.BeginForm()){
foreach(var recipient in Model)
{
@Html.Partial("_Recipient", recipient)
}
Add
}
,并将包括一个脚本来为新的CashRecipientVMvar url = '@Url.Action("Recipient")';var form = $('form');var recipients = $('#recipients');$('#add').click(function() {
$.get(url, function(response) {
recipients.append(response);
// Reparse the validator for client side validation
form.data('validator', null);
$.validator.unobtrusive.parse(form);
});});
以及删除项的脚本$('.delete').click(function() {
var container = $(this).closest('.recipient');
var id = container.find('.id').val();
if (id) {
// make ajax post to delete item
$.post(yourDeleteUrl, { id: id }, function(result) {
container.remove();
}.fail(function (result) {
// Oops, something went wrong (display error message?)
}
} else {
// It never existed, so just remove the container
container.remove();
}});
表单将发回public ActionResult Create(IEnumerable recipients)