【发布时间】:2015-03-12 20:22:01
【问题描述】:
我有一个MVC模型如下
public class ListSampleModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int SampleId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<PointOfContact> lstPoc { get; set; }
}
public class PointOfContact
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int PocID { get; set; }
public string EmailAddress { get; set; }
public string PhoneNumber { get; set; }
}
我所做的是,在 jquery 对话框上创建“PointOfContact”作为局部视图,然后在“保存”按钮上单击它在标签中的主视图上显示数据(我将有多个联系点),现在提交我希望将此数据与 ListSampleData 的属性值一起发送回控制器。 问题是,与简单属性相关的数据被返回,但列表始终为空。
下面是我的视图
@model MVCDataFlowSample.Models.ListSampleModel
@using (Html.BeginForm("Create", "ListSample", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ListSampleModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div id="dialog1" class="ui-dialog" style="background-color:gray;"></div>
<div id="data">
</div>
<p>
<input type="button" value="Add More..." id="btnAdd" />
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
主视图上的 Javascript
<script type="text/javascript">
$(document).ready(function () {
$('#btnAdd').on('click', function () {
$('#dialog1').dialog({
autoOpen: true,
position: { my: "center", at: "center", of: window },
width: 1000,
resizable: false,
title: 'Add User Form',
modal: true,
open: function () {
$(this).load('@Url.Action("PocPartial", "ListSample")');
},
buttons: {
"Save User": function () {
addUserInfo();
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
return false;
});
function addUserInfo(email, phone) {
var text = "<div id='EmailAddress'>Email Address:" + $("#EAddress").val() + "</div><div id='PhoneNumber'>Phone Number:" + $("#PhNo").val() + "</div>";
$("#data").append(text);
}
});
</script>
局部视图
@model MVCDataFlowSample.Models.PointOfContact
<div>
@Html.Label("EmailAddress:")
<div>
@Html.TextBoxFor(x => x.EmailAddress, new { id = "EAddress" })
</div>
@Html.Label("PhoneNumber:")
<div>
@Html.TextBoxFor(x => x.PhoneNumber, new { id = "PhNo" })
</div>
</div>
我们将不胜感激。
【问题讨论】:
-
Wich list is aways null?
-
IList
-
您必须在 addUser 信息中创建一个列表,如下所示:stackoverflow.com/questions/9306246/…。您还必须将其附加到已发布的表单中
-
谢谢@Fals ...我不知道为什么我找不到那个帖子..会尝试那个解决方案。