I have a Model with Child model.
[Table("Personnel")]
public class Personnel
{
[Key]
public int Id { get; set; }
[MaxLength(10)]
public string Code { get; set; }
[MaxLength(20)]
public string Name { get; set; }
public virtual List<PersonnelDegree> Degrees
{
get;
set;
}
}
public class PersonnelDegree
{
[Key]
public int Id { get; set; }
[ForeignKey("Personnel")]
public int PersonnelId { get; set; }
public virtual Personnel Personnel { get; set; }
[UIHint("Enum")]
public Degree Degree { get; set; }
public string Major { get; set; }
public string SubField { get; set; }
public string Location { get; set; }
}
I want to created a view for this.(Add)
I added pesonnel field to view, but how to add items for PersonnelDegree?
@using (Html.BeginForm("Add", "Personnel", FormMethod.Post, new {enctype = "multipart/form-data", @class = "form-horizontal tasi-form", id = "default"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, null, new {@class = "alert alert-danger "})
<div class="form-group">
@Html.LabelFor(m => m.Code, new {@class = "control-label col-lg-1"})
<div class="col-lg-3">
@Html.TextBoxFor(m => m.Code, null, new {@class = "form-control", maxlength = 10})
@Html.ValidationMessageFor(m => m.Code)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Name, new {@class = "control-label col-lg-1"})
<div class="col-lg-3">
@Html.TextBoxFor(m => m.Name, new {@class = "form-control", maxlength = 20})
@Html.ValidationMessageFor(m => m.Name)
</div>
@Html.LabelFor(m => m.Family, new {@class = "control-label col-lg-1"})
<div class="col-lg-3">
@Html.TextBoxFor(m => m.Family, null, new {@class = "form-control", maxlength = 30})
@Html.ValidationMessageFor(m => m.Family)
</div>
</div>
Can i add multy PersonnelDegrees in this View?
Edit
I add a div in view for Degrees
<div id="Degrees">
<div id="NewDegree" style="display:none">
<div class="form-group">
<input class="form-control" id="Degrees[#].Major" name="Degrees[#].Major" value="" type="text">
// another items
</div>
</div>
</div>
and in javascript :
$(document).ready(function() {
$(function() {
$("#addItem").click(function () {
var index = $('#Degrees tbody tr').length; // assumes rows wont be deleted
var clone = $('#NewDegree').html();
// Update the index of the clone
clone.replace(/\[#\]/g, '[' + index + ']');
clone.replace(/"%"/g, '"' + index + '"');
$('#Degrees').append(clone);
});
);
});
it add a div ,But after a few seconds hide div and refresh page.