问题
I have a form that has a section where it uses jQuery tab generated by BeginCollectionItem. This is done by one-to-many relationship. I can create multiple tabs within the form and can submit it (newForm.cshtml). However, when it comes to editing, especially when I have to retrieve the form (with multiple tabs filled with information), I am kinda stuck here.
So the scenario is simple. I have a table that shows the list of submitted forms and can click an edit link of a row to edit that retrieved form. At this moment, I can only retrieve the Name in NewForm, but other Names associated with Family tabs are not retrieved.
Model:
public class NewForm
{
public int NewFormId { get; set; }
public string Name { get; set; }
public ICollection<Family> Families{ get; set; }
}
public class Family
{
public int FamilyId { get; set; }
public string Name { get; set; }
public int NewFormId { get; set; }
public virtual NewForm NewForm{ get; set; }
}
Edit.cshtml:
@model Test.Models.NewForm
@using (Html.BeginForm("Edit", "NewForm", FormMethod.Post))
{
<p>Your Name</p>
@Html.EditorFor(m => m.Name)
<button type="button" id="addFamily" name="addFamily">Add Family</button>
<div id="tabs">
<ul>
<li><a href="#tabs-family">Family 1</a></li>
</ul>
<div id="tabs-family">
@Html.Action("AddNewFamily", "NewForm")
</div>
</div>
<button type="submit">Save</button>
}
<script>
$(function () {
var tabTemplate = "<li><a href='#{href}'>#{label}</a></li>",
familyTabCounter = 2,
var tabs = $("#tabs").tabs();
$('#addFamily').on('click', function (event) {
$.ajax({
url: '/NewForm/AddNewFamily',
}).success(function (partialView) {
addTab(partialView, this.url);
});
event.preventDefault();
});
// Actual addTab function: adds new tab using the input from the form above
function addTab(partialView, url) {
if (url == "/NewForm/AddNewFamily") {
var label = "Family " + familyTabCounter,
id = "tabs-family" + familyTabCounter,
li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label));
tabs.find(".ui-tabs-nav").append(li);
tabs.append("<div id='" + id + "'><p>" + partialView + "</p></div>");
tabs.tabs("refresh");
familyTabCounter++;
}
}
});
</script>
Family.cshtml:
@model Test.Models.Family
<div class="editorRow">
@using (Html.BeginCollectionItem("families"))
{
@Html.EditorFor(m => m.Name)
}
</div>
Controller:
public PartialViewResult AddNewFamily()
{
return PartialView("~/Views/NewForm/PartialView/Family.cshtml");
}
public ActionResult Edit(int? id)
{
NewForm form = db.NewForms
.Include(i => i.Families)
.Where(x => x.NewFormId == id)
.Single();
return View(form);
}
How can I retrieve information for families(tabs) associated with newForm?
Edit
@using (Html.BeginForm("Edit", "NewForm", FormMethod.Post))
{
<p>Your Name</p>
@Html.EditorFor(m => m.Name)
<button type="button" id="addFamily" name="addFamily">Add Family</button>
@{int number = 1;}
@foreach (var family in Model.Families)
{
<div id="tabs">
<ul>
<li><a href="#tabs-family">Family @number</a></li>
</ul>
<div id="tabs-family">
@Html.Partial("../NewForm/Family", family)
</div>
</div>
number++;
}
<button type="submit">Save</button>
}
I've reflected Stephen's advice and retrieving multiple families is working, but now I face another problem that I can't create each tab for each family. I tried with my code, but it creates tab, but it is placed in a weird place. Any suggestions?
回答1:
You need to generate a tabs for each Family
in your collection using 2 loops,
one to create the headers (the <li><a>
elements), and one for the content (the <div>
elements.
Note that your current implementation is generating duplicate id
attributes, and you need to ensure that each content <div>
has a unique id
attribute, and that the associated header <a>
element has matching href
value.
@model Test.Models.NewForm
@{
// Initialize values for loops
int index = 1;
int count = Model.Families.Count() + 1;
}
@using (Html.BeginForm("Edit", "NewForm", FormMethod.Post))
{
<p>Your Name</p>
@Html.EditorFor(m => m.Name)
<button type="button" id="addFamily" name="addFamily">Add Family</button>
<div id="tabs">
<ul>
// Create the tab header for each Family
@for(int i = index; i < count; i++)
{
// Generate unique fragment identifier
string id = String.Format("#family-{0}", i);
<li><a href="@id">Family @i</a></li>
}
</ul>
// Create the content (partial view) for each Family
@foreach(var family in Model.Families)
{
// Generate unique ID
string id = String.Format("family-{0}", index);
<div id="@id">
@Html.Partial("Family", family)
</div>
index++;
}
</div>
<button type="submit">Save</button>
}
Side note: In your current script, you need to set the familyTabCounter
using
var familyTabCounter = @index
so that clicking the 'Add' button will generate the correct id
attribute and associated fragment identifier based on the number of existing Family
in the collection.
来源:https://stackoverflow.com/questions/40688571/asp-net-mvc-retrieving-one-to-many-relationship-using-jquery-tab