0
我正在尝试创建一个页面来编辑'Person',但是我遇到了如何编辑List对象的问题(通过编辑,我的意思是如何动态地添加一个电子邮件到名单)。研究在线以及在stackoverflow已导致我编辑模板,然后动态添加项目与Ajax。但是,我错过了某处的连接。要么为新电子邮件创建未绑定的空文本框,要么获取空引用错误。编辑列表<String> MVC 4
型号:
[DynamoDBTable("people")]
public class Person
{
[DynamoDBHashKey]
[DynamoDBProperty(AttributeName = "name")]
public string Name{ get; set; }
[DynamoDBRangeKey]
[DynamoDBProperty(AttributeName = "id")]
public string ID { get; set; }
[DynamoDBProperty(AttributeName = "emails")]
public List<string> Emails{ get; set; }
public Person()
{
}
}
查看:
<div id="emails" class="row">
<div class="form-group col-xs-6 col-sm-6">
@Html.LabelFor(x => x.Emails)
@Html.EditorFor(x => Model.Emails, new { @class = "form-control"})
</div>
<button id="addEmail">Add</button>
</div>
<script type="text/javascript">
$("#addEmail").on('click', function (event) {
event.preventDefault();
$.ajax({
async: false,
url: '/controller/newEmail'
}).success(function (partialView) {
$('#emails').append(partialView);
});
});
</script>
EditorTemplate - 人:
@model Models.Person
@for (int i = 0; i < Model.Emails.Count(); i++)
{
@Html.EditorFor(x => Model.Emails)
}
EditorTemplate - 字符串:
@model string
@Html.TextBoxFor(model => Model)
控制器:
public ActionResult newEmail()
{
var emails = new Person().Emails;
return PartialView("~/Views/Shared/EditorTemplates/string.cshtml", emails);
}
你不需要阿贾克斯。您只需使用javascript/jquery将一个新输入 - “'添加到DOM中。 –