سلام دوستان
ممکنه راهنمایی کنید کجای این کد درست نیست چون نمیتونم CRUD رو با دیالوگ انجام بدم
از این لینک استفاده کردم
http://www.mitechdev.com/2016/03/crud-operations-in-aspnet-mvc-using-jquery-and-EF-part1.html
@model IEnumerable<CustomersClub.Models.Departeman> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <script src="~/Scripts/jquery-1.12.4.min.js"></script> <script src="~/Scripts/jquery-ui-1.12.1.min.js"></script> <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-latest.min.js"></script> <script> $(document).ready(function () { var url = ""; $("#dialog-alert").dialog({ autoOpen: false, resizable: false, height: 170, width: 350, show: { effect: 'drop', direction: "up" }, modal: true, draggable: true, open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }, buttons: { "OK": function () { $(this).dialog("close"); }, "Cancel": function () { $(this).dialog("close"); } } }); if ('@TempData["msg"]' != "") { $("#dialog-alert").dialog('open'); } $("#dialog-edit").dialog({ title: 'Create User', autoOpen: false, resizable: false, width: 400, show: { effect: 'drop', direction: "up" }, modal: true, draggable: true, open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); $(this).load(url); } }); $("#dialog-confirm").dialog({ autoOpen: false, resizable: false, height: 170, width: 350, show: { effect: 'drop', direction: "up" }, modal: true, draggable: true, open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }, buttons: { "OK": function () { $(this).dialog("close"); window.location.href = url; }, "Cancel": function () { $(this).dialog("close"); } } }); $("#dialog-detail").dialog({ title: 'View User', autoOpen: false, resizable: false, width: 400, show: { effect: 'drop', direction: "up" }, modal: true, draggable: true, open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); $(this).load(url); }, buttons: { "Close": function () { $(this).dialog("close"); } } }); $("#lnkCreate").live("click", function (e) { //e.preventDefault(); //use this or return false url = $(this).attr('href'); $("#dialog-edit").dialog('open'); return false; }); $(".lnkEdit").live("click", function (e) { // e.preventDefault(); use this or return false url = $(this).attr('href'); $(".ui-dialog-title").html("Update User"); $("#dialog-edit").dialog('open'); return false; }); $(".lnkDelete").live("click", function (e) { // e.preventDefault(); use this or return false url = $(this).attr('href'); $("#dialog-confirm").dialog('open'); return false; }); $(".lnkDetail").live("click", function (e) { // e.preventDefault(); use this or return false url = $(this).attr('href'); $("#dialog-detail").dialog('open'); return false; }); $("#btncancel").live("click", function (e) { $("#dialog-edit").dialog("close"); return false; }); }); </script> <div id="dialog-alert" style="display: none"> <p> @TempData["msg"]! </p> </div> <div id="dialog-confirm" style="display: none"> <p> <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span> Are you sure to delete? </p> </div> <div id="dialog-edit" style="display: none"> </div> <div id="dialog-detail" style="display: none"> </div> <p> @Html.ActionLink("سطر جدید", "Create", null, new { id = "lnkCreate" }) </p> <table class="table" style="font-family:'B Nazanin'"> <tr> <th> @Html.DisplayNameFor(model => model.DepartemanName) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.DepartemanName) </td> <td class="btn btn-info " > @Html.ActionLink("ویرایش", "Edit", new { id = item.DepartemanID }, new { @class = "lnkEdit" }) | </td> <td class="btn btn-warning" > @Html.ActionLink("جزئیات", "Details", new { id = item.DepartemanID }, new { @class = "lnkDetail" }) | </td> <td class="btn btn-danger"> @Html.ActionLink("حذف", "Delete", new { id = item.DepartemanID }, new { @class = "lnkDelete" }) </td> </tr> } </table>
@model CustomersClub.Models.Departeman @{ ViewBag.Title = "Edit"; } <h2>Edit</h2> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset> <legend>Departeman</legend> @Html.HiddenFor(model => model.DepartemanID) <div class="editor-label"> @Html.LabelFor(model => model.DepartemanName) </div> <div class="editor-field"> @Html.EditorFor(model => model.DepartemanName) @Html.ValidationMessageFor(model => model.DepartemanName) </div> <p> <input type="submit" value="Save" /> <input type="submit" value="Cancel" id="btncancel" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Departeman departeman = db.Departemans.Find(id); if (departeman == null) { return HttpNotFound(); } return View(departeman); } // POST: Departemen/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "DepartemanID,DepartemanName")] Departeman departeman) { if (ModelState.IsValid) { db.Entry(departeman).State = EntityState.Modified; db.SaveChanges(); TempData["msg"] = "Data Updated Successfully!"; return RedirectToAction("Index"); } return View(departeman); }
با سلام
دوست عزیز اول بگید کجای کدتون کار نمکنه که بتونیم راهنمایی کنبم . دوم شما باید partial views برگردونید نه view .
ممنون دوست عزیز
کد کار میکنه ولی فرم رو به صورت اجکسی نمیبینم
دوست عزیز شما اصلا از ajax استفاده نکردید که بصورت ajax مشاهده کنید
function EditPollFunction() { $.ajax({ method: "POST", url: "@Url.Action("EditPoll")", data: { ID: $("#EditOptionID").val(), SurveyID: '@Model.ID', OptionTitle: $("#EditOptionTitle").val() }, success: function (result) { if (result == "OK") { var ps = new PNotify({ title: 'پیام سیستم', text: 'عملیات با موفقیت انجام شد.', type: 'success', shadow: true, icon: 'icon-3x icon-pencil' }); $("#OptionTitle").val(''); Update(); } else { var pe = new PNotify({ title: 'پیام سیستم', text: 'عملیات با خطا مواجه شد.', type: 'error', shadow: true, icon: 'icon-3x icon-ban-circle' }); } } }); }
این کدی هست که من برای ویرایش به صورت ajax استفاده کردم البته لازم به بگم از modal موجود در boostrap استفاده کردم و کد به یک دکمه که داخل modal هست bind شده
فکر کنم modified رو در update باید بنویسی
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)