I have a Personal GPA Tracker project to work on, and i'm trying to get a modal to pop up from my CRUD functions. I have it semi working wherein it returns a partial view, but not in modal form. I think my logic is messed up somewhere or i'm not using JQuery and Ajax correctly or something. Any help would be appreciated.
Here is what I have so far.
Home Controller
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Course course = _db.Courses.Find(id);
if (course == null)
{
return HttpNotFound();
}
return PartialView("_Edit", course);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Course course)
{
if (ModelState.IsValid)
{
_db.Entry(course).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
CourseList Partial called from Index
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id, @data_toggle="modal", @data_target="editCourseModal" } ) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
Script inside of the CourseList
<script type="text/javascript">
$(function() {
$('editCourseModal').on("click", function(e) {
$('formEditCourse').load(this.href, function() {
$('editCourseModal').modal({
keyboard: true
}, 'show');
})
})
$("#editBtn").on("click", function() {
$("#formEditCourse").submit();
});
$("#formEditCourse").on("submit", function(event) {
var $form = $(this);
var options = {
url: $form.attr("action"),
type: $form.attr("method"),
data: $form.serialize()
};
});
});
</script>
Edit Partial
@model Project3.Models.Course
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div id="mainContent" class="modal-content col-sm-12">
<div id="myModalContent"></div>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Edit Course</h4>
</div>
@using (Html.BeginForm()) { @Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.courseCode, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.courseCode, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.courseCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.courseName, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.courseName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.courseName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.courseHours, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.courseHours, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.courseHours, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.courseLetterGrade, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.courseLetterGrade, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.courseLetterGrade, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Cancel</button>
<input class="btn btn-primary" type="submit" value="Save" />
</div>
</div>
</div>
</div>
}
Again, any help would be greatly appreciated.