嗨,我正在尝试向表单中随机添加部分视图 但是,当我进入控制器时,我从未获得任何数据, 我得到一个空引用。
Index.cshtml
array (size=3)
'Jan' =>
array (size=2)
'x' => string 'Jan' (length=3)
'y' => int 120
'Feb' =>
array (size=2)
'x' => string 'Feb' (length=3)
'y' => int 50
'Mar' =>
array (size=2)
'x' => string 'Mar' (length=3)
'y' => int 70
我试图将Familymember.cshtml添加到Index.cshtml表单中的一组输入字段
@using (Html.BeginForm("SendFamilyMembers", "ContactUs", FormMethod.Post))
{
<div id="addMoreMembers" class="row">
</div>
<div class="form-group col-sm-12">
<div class="row">
<div class="col-sm-12">
<label id="LblAddFamilyMemberButton" class="btn btn-default btn-file">
+Add Family Members
</label>
</div>
</div>
</div>
<div class="form-group col-sm-12">
<div class="row">
<div class="col-sm-12">
<input id="Send" type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
}
我的indexViewModel.cs视图模型
@model Contact_Portal.Models.ViewModel.FamilyMembersViewModel
<div class="form-group col-sm-6">
<div class="row">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @for = "Name_" + Model.count, @class = "control-label col-md-2" })
<div class="col-sm-6">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { id = "Name_" + Model.count, @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group col-sm-6">
<div class="row">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @for = "Email_" + Model.count, @class = "control-label col-md-2" })
<div class="col-sm-6">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { id = "Email_" + Model.count, @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
</div>
_layout.cshtml
public class AddFamilyMembersViewModel
{
[Required()]
[Display(Name = "Navn:")]
public string Name { get; set; }
[Display(Name = "Email:")]
public string Email { get; set; }
public List<FamilyMembersViewModel> FamilyMembers { get; set; } = new List<FamilyMembersViewModel>();
}
in my layout I have the following javascript to take care of adding the amount of input fields the user would like.
我家庭的viewmodelMembersViewModel.cs
$(document).on("click", "#LblAddFamilyMemberButton", function (e) {
e.preventDefault();
familyMemberCount = familyMemberCount + 1;
$.ajax({
// url: "/ContactUs/GetForm",
url: '@Url.Action("AddFamilyMember", "ContactUs")',
type: "POST",
data: { count: familyMemberCount } ,
async: true,
success: function (data) {
var element = document.getElementById("addMoreMembers");
$(element).append(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("An error has occured!!! " + xhr.status + " && " + xhr.responseText);
}
});
});
在我的控制器中,如果我尝试访问列表中的第一个位置,则会得到空引用,因为列表为空。 ContactUsController.cs
public class FamilyMembersViewModel
{
public int count { get; set; }
[Display(Name = "Navn:")]
public string Name { get; set; }
[Display(Name = "Email:")]
public string Email { get; set; }
}
那我的错误是什么?
答案 0 :(得分:0)
<div class="dropdown-list clearfix traveller-list">
<div class="pull-left">
@Html.CheckBoxFor(m => m.DependentParents[i].IsSelected)
<label for=@Model.DependentParents[i].DisplayText class="txtShdws">@Model.DependentParents[i].DisplayText</label>
</div>
<div class="pull-right age-dropdown">
@Html.TextBoxFor(m => m.DependentParents[i].TravellerAge, new { @class =
"custom-form", @placeholder = "Age" })
</div>
</div>
@Html.ValidationMessageFor(m => m.DependentParents[i].TravellerAge)