I am new to MVC. Hi have done small project. when i click on edit actionlink it should popup i wrote its working fine. when popup displays i changed the data in edit popup when i click on save button. its not disappearing. please help me
here is my code. here i kept editorstemplate one folder in that folder Editinput.cshtml it will display all fields when i click on edit.
Movie Controller.
public ActionResult Edit(int id)
{
Movies movies = db.Movies.Find(id);
return PartialView(movies);
}
[HttpPost]
public ActionResult Edit(Movies movies)
{
if (ModelState.IsValid)
{
if (Request.IsAjaxRequest())
{
db.Entry(movies).State = EntityState.Modified;
db.SaveChanges();
return View("Index", movies);
}
}
return RedirectToAction("Index");
}
in Index.cshtml.
@model IEnumerable<MVCSampleProject.Models.Movies>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Title
</th>
<th>
ReleaseDate
</th>
<th>
Genre
</th>
<th>
Price
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { @class="openDialog",data_dialog_id="emailDialog", data_dialog_title="Edit"})
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
in Edit.cshtml.
@model MVCSampleProject.Models.Movies
<h2>Edit</h2>
@Html.Partial("_Edit")
_Edit.cshtml
@model MVCSampleProject.Models.Movies
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "emailDialog" }))
{
@Html.ValidationSummary(true)
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
}
EditInput.cshtml.cs
@model MVCSampleProject.Models.Movies
<h2>Editinfo</h2>
<fieldset>
<legend>Movies</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Genre)
@Html.ValidationMessageFor(model => model.Genre)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
</fieldset>
}
here my javascript code
<script type="text/javascript">
$.ajaxSetup({ cache: false });
$(document).ready(function () {
$(".openDialog").live("click", function (e) {
e.preventDefault();
$("<div></div>")
.addClass("dialog")
.attr("id", $(this)
.attr("data-dialog-id"))
.appendTo("body")
.dialog({
title: $(this).attr("data-dialog-title"),
close: function () { $(this).remove() },
modal: true
})
.load(this.href);
});
$(".close").live("click", function (e) {
e.preventDefault();
$(this).closest(".dialog").dialog("close");
});
});
</script>
please help me thank you.
regards
Sreekanth.