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中。 –