我的MVC Web应用程序出现问题,添加了在客户端创建的新记录。当我单击提交提交按钮时,传递给控制器的视图模型仅包含已存在的记录,而不包含由javascript添加的新记录。 这是我使用的代码的简化版本: -
服务器代码
public class person
{
public int ID { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
}
public class viewmodel
{
List<person> people { get; set; }
}
视图
<div id="dv_people">
@{
for (c = 0; c < Model.people.Count(); c++)
{
@Html.EditorFor(m => Model.people[c].firstname);
@Html.EditorFor(m => Model.people[c].lastname);
@Html.HiddenFor(m => Model.people[c].ID);
}
}
</div>
的Javascript
function addPerson(firsname, lastname, uniqueIndex){
var htm = '<input class="text-box single-line" id="people_' + uniqueIndex + '__firstname" name="people[' + uniqueIndex + '].firstname" type="text" value="' + firsname + '">'
htm = htm + '<input class="text-box single-line" id="people_' + uniqueIndex + '__lastname" name="people[' + uniqueIndex + '].lastname" type="text" value="' + lastname + '">'
htm = htm + '<input id="people_' + uniqueIndex + '__ID" name="people[' + uniqueIndex + '].ID" type="hidden" value="' + uniqueIndex + '">'
$("#dv_poeple").append(htm)
}