C# 在ASP.NETMVC中创建多个模型
我使用的是代码优先。 模型的简化版本:C# 在ASP.NETMVC中创建多个模型,c#,asp.net,.net,asp.net-mvc,C#,Asp.net,.net,Asp.net Mvc,我使用的是代码优先。 模型的简化版本: public class Person { public int PersonID { get; set; } public string Name { get; set; } public List<Phone> Phones { get; set; } } public class Phone { public int PhoneID { get; set; } public string Nu
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public List<Phone> Phones { get; set; }
}
public class Phone
{
public int PhoneID { get; set; }
public string Number { get; set; }
public virtual Person Person{ get; set; }
public int PersonID { get; set; }
}
公共类人物
{
公共int PersonID{get;set;}
公共字符串名称{get;set;}
公共列表电话{get;set;}
}
公用电话
{
公共int PhoneID{get;set;}
公共字符串编号{get;set;}
公共虚拟人{get;set;}
公共int PersonID{get;set;}
}
所以我在人和电话之间有一对多的关系
我想使用户能够在CreateView for person中添加尽可能多的手机。当该视图中的表单提交时,我将保存该用户和添加的所有手机
谁能给我指出正确的方向吗?我读过关于模板编辑器的文章,这是正确的用例吗?您可以像这样使用viewModel
public class PersonAddViewModel
{
public string Name{set;get;}
public List<string> Phones{set;get;}
}
})) 可能是重复如何在mvc 5中插入主细节表可能只是一些javascript,因为当他们单击
+
按钮时,在formI上添加另一个输入元素。我有这个部分,更多的是在添加的输入中获取他们键入的内容并将其保存到db,这是我的问题。谢谢你!有关一些选项,请参阅。
@using (Html.BeginForm("Add", "Person", FormMethod.Post }))
{
<div class="col-md-6">
<div class="form-group">
@Html.LabelFor(p => p.Name)
@Html.EditorFor(p => p.Name, new { @class = "form-control" })
</div>
</div>
<div class="col-md-6" id="#row-0">
<div class="form-group">
@Html.LabelFor(p => p.Phones[0])
@Html.EditorFor(p => p.Phones[0], new { @class = "form-control" })
</div>
</div>
<div id=OtherPhones></div>
<button type="button" class="btn btn-danger btn-float btn-float-lg btn- rounded" id="add"><i class="icon-plus2"></i></button>
<button type=submit>Submit</button>
}
$("#add").click(function () {
inputCount++;
var newDiv = $("#row-0").clone();
newDiv.attr('id', 'row-' + inputCount);
newDiv.find("input,select").each(function () {
$(this).attr({
'name': function (_, name) { return name.toString().replace('0', inputCount) },
'id': function (_, id) { return id.toString().replace('0', inputCount) },
'item': function (_, item) { return item.toString().replace('0', inputCount) }
});
var type = $(this).attr('type');
if (type === 'hidden') {
$(this).val('0');
} else {
$(this).val('');
}
});
newDiv.find(".available").each(function () {
$(this).attr({
'id': function (_, id) { return id.toString().replace('0', inputCount) }
});
});
newDiv.find(".delCol").each(function () {
$(this).find('button').attr('item', inputCount);
$(this).fadeIn();
});
newDiv.find("label").each(function () {
$(this).attr({
'for': function (_, id) { return id.toString().replace('0', inputCount) }
});
});
newDiv.find(".field-validation-valid").each(function () {
$(this).attr({
'data-valmsg-for': function (_, id) { return id.toString().replace('0', inputCount) }
});
}).end();
$("#Other").append(newDiv);
$('form').data('validator', null);
$.validator.unobtrusive.parse('form');