Displaying product names in a view, with each row of the table having the product name, an edit button, and a delete button; there is also a button to add a new product. Instead of having the controller actions redirect to a different view when the 'Add' or 'Edit' buttons are clicked, can I have a modal pop up instead? I tried this solution from @Shane Courtrille and this one from @Darin Dimitrov, but haven't been able to get it to work; nothing shows up when I click either button. Here's my code:
Views > Products > Index:
<button id="add" class="btn btn-primary modalButton" data-url="@Url.Action("New")">Add Product</button>
<td>
<a href="#" data-url="@Url.Action("Edit", "Products", new {id = p.Name})" class="glyphicon glyphicon-pencil modalButton" id="edit"></a>
</td> //This is within for loop to display products
<div id="productModal" class="modal hide fade in">
<div id="productContainer"></div>
</div>
<script>
$(document)
.ready(function() {
$('.modalButton').click(function() {
var url = $(this).data('url');
$.get(url, function(data) {
$('#productContainer').html(data);
$('#productModal').removeClass('hide');
});
});
});
</script>
Within ProductsController:
[HttpGet]
public ActionResult New()
{
var vm = new MyViewModel();
return PartialView("_MyModal", vm);
}
[HttpGet]
public ActionResult Edit(decimal id)
{
var product = _db.Product.SingleOrDefault(p => p.Id == id);
if(product == null)
return HttpNotFound();
var vm = new MyViewModel(product);
return PartialView("_MyModal", vm);
}
Finally, within _MyModal.cshtml
@model MyProject.ViewModels.MyViewModel
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button data-dismiss="modal" class="close"><span>×</span></button>
<div class="modal-title">@Model.Title</div>
</div>
<div class="modal-body">
This is my test modal
</div>
<div>
<button name="Modify" class="btn btn-primary" onclick="if (confirm('Are you sure you want to save?')) return @Url.Action("Save");">Save</button>
<button class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>