The current situation: For the essence of this problem, I have two database tables/models. I'm using an AJAX form to update the displayed model data in list form from each table when a new model is created and added. In effect, the page never has to reload to display any changes to the lists.
I'm displaying the create form statically for each model above the list of items. This works fine for a model that has no foreign key dependencies. However, one of my models also has a select list in the create form that is tied to the other model list on the page.
The problem: When I add a record for the simpler model, the select list for the create form of the model with the foreign key dependency does not update to include the changes. I know why it doesn't update and I think that I need to use AJAX to either recreate the create form in-place entirely or update just the select list.
Is there a way to use the AJAX form to not only update the list of the model that I'm adding an item to but also update the div that contains the create form at the same time?
I believe I have included all the relevant code to hopefully give a clearer picture of what I'm asking
Sample Create form for the simpler model:
@using (Ajax.BeginForm("_CreateCategory", new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "list-categories"
}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-2">Name: </label>
<div class="col-md-5">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
<div class="col-md-5">
<input type="submit" value="Add" class="btn btn-success" />
</div>
</div>
</div>
}
Sample model with foreign key dependency
@using (Ajax.BeginForm("_CreateType", new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "list-types"
}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-2">Name: </label>
<div class="col-md-offset-1 col-md-6">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Category: </label>
<div class="col-md-6">
@Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
</div>
<div class="col-md-3">
<input type="submit" value="Add" class="btn btn-success" />
</div>
</div>
</div>
}
Sample list partial view for the model with the foreign key
<div id="list-types">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Category.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category.Name)
</td>
<td>
@using (Ajax.BeginForm("DeleteType", new { id = item.ID }, new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "list-types"
}))
{
@Html.AntiForgeryToken()
<button type="submit" class="no-default">
<span class="glyphicon glyphicon-remove"></span>
</button>
}
</td>
</tr>
}
</table>
</div>
Controller action for adding simpler model
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _CreateCategory([Bind(Include = "ID, Name")] Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
}
var categories = db.Categories;
return PartialView("_ListCategories", categories.ToList());
}
Sample Index page code that displays the lists and create forms
<div class="col-lg-4 block">
<h3>Categories</h3>
@Html.Action("_CreateCategory")
@Html.Action("_ListCategories")
</div>
<div class="col-lg-4 block">
<h3>Types</h3>
@Html.Action("_CreateType")
@Html.Action("_ListTypes")
</div>