0

I have a view which content Detail for particular clinic and list of doctors. List of doctors have a Delete action. I want to refresh detail of clinic (like update panel) using Ajax Helper but complete web page is loading.

Below is my web page:

enter image description here

I have tried Ajax.Action, but it is not working :

My View:

@model AddMore.Models.Clinics
@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>
<div id ="divActions">
    <fieldset>
        <legend>My Details</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Contact)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.Contact)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.Address)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.PinCode)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.PinCode)
        </div>    

    </fieldset>

    <fieldset>
        <legend>Doctors</legend>

        @if (ViewBag.DoctorList != null)
        {
            <table>
                <tr>
                    <th>Doctor Name</th>
                    <th>Email Adress</th>
                    <th>Phone</th>
                    <th></th>
                </tr>
                @foreach (AddMore.Models.EditDoctors docs in ViewBag.DoctorList)
                {
                    <tr>
                        <td>@docs.DocName</td>
                        <td>@docs.DocEmail</td>
                        <td>@docs.DocHandphoneNumber</td>
                        <td>@if(docs.IsPrimary == false)
                            { 

@*@Html.ActionLink("Delete", "OtherDoctorDelete", new { id= docs.DocId, clinicid = Model.Id })*@

                            @Ajax.ActionLink("Delete", "OtherDoctorDelete", "Clinic", new { id= docs.DocId, clinicid = Model.Id}, new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "divActions",
Confirm = "Are you sure to delete?"
})

                        }</td>
                    </tr>
                }
            </table>
        }
    </fieldset>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id= Model.Id }) &nbsp;&nbsp; @Html.ActionLink("Back to list", "Index")
    </p>

My Action:

   public ActionResult Details(int id = 0)
        {
            Clinics clinic = db.Clinic.Find(id);


            IList<Doctors> doctors = (from d in db.Doctor
                                      where d.ClinicId == id
                                      orderby d.Name descending
                                      select d).ToList();
            List<EditDoctors> doctorlist = new List<EditDoctors>();
            EditDoctors edoc;
            foreach (Doctors odoc in doctors)
            {
                edoc = new EditDoctors();
                edoc.ClinicId = odoc.ClinicId;
                edoc.DocId = odoc.Id;
                edoc.IsPrimary = odoc.IsPrimary;
                edoc.DocName = odoc.Name;
                edoc.DocHandphoneNumber = odoc.HandphoneNumber;
                edoc.DocEmail = odoc.Email;
                doctorlist.Add(edoc);
            }

            ViewBag.DoctorList = doctorlist;    

            return View(clinic);

        }

Please tell me what is I am doing wrong.

Sorry about bad English!!

After Matt's Help

I tried following:

@if (ViewBag.DoctorList != null)
        {
            <table>
                <tr>
                    <th>Doctor Name</th>
                    <th>Email Adress</th>
                    <th>Phone</th>
                    <th></th>
                </tr>

               @using (Html.BeginForm())
{

                foreach (AddMore.Models.EditDoctors docs in ViewBag.DoctorList)
                {
                    <tr>
                        <td>@docs.DocName</td>
                        <td>@docs.DocEmail</td>
                        <td>@docs.DocHandphoneNumber</td>
                        <td>@if (docs.IsPrimary == false)
                            { 

                   <input type="Submit" id="@docs.DocId" value="Delete" class="btnDelete" />

                            <script>

                                $(document).ready(function () {
                                    $('.btnDelete').on('click', function () {
                                        var tr = $(this).closest('tr');
                                        $.ajax({
                                            url: '@(Url.Action("Delete", "OtherDoctorDelete", new {id= docs.DocId, clinicid = Model.Id }))',
            type: 'post',
            data: {
                Id: $(this).closest('tr').attr('id') //should be able to simplify this to tr.attr('id') but this will work
            },
            success: function (result) {
                //can just do a tr.remove but the link I posted above adds some style to it
                tr.css("background-color", "#FF3700");
                tr.fadeOut(400, function () {
                    tr.remove();
                });
            }
        });
    });
                            });
                            </script>
                            }</td>
                    </tr>
                }
               }
            </table>
        }

and

[HttpPost]
        public bool OtherDoctorDelete(int id, int clinicid)
        {
            Doctors doc = db.Doctor.Find(id);
            db.Doctor.Remove(doc);
            db.SaveChanges();
            return true;
            //return RedirectToAction("Details", new { id = clinicid });
        }

these are scripts:

<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

Now delete is not working. I think I am doing something wrong, please help.

NMathur
  • 829
  • 1
  • 17
  • 35
  • 2
    From what I can see you don't need to update the entire view. You should be able to do an ajax call to delete the doctor and then just remove the row from the table on the view. Would that work for you? – Matt Bodily Nov 11 '13 at 15:48
  • @Matt yes it will, but I don't know how to do that .. as I have posted above, my action is returning model for clinic info and a ViewBag for doctors list, How can I remove individual row? Any suggestions? – NMathur Nov 12 '13 at 04:13
  • If my answer helped you, would you mind marking it as the correct answer? – Matt Bodily Nov 15 '13 at 14:26
  • @matt .. hi matt .. thanks a lot for reply ... sorry I have not get chance to try this out .. I will try to implement this as soon as I get time . and if this is helpful I will surely mark this as answer ... thanks again :) – NMathur Nov 18 '13 at 04:26

1 Answers1

1

jquery delete table row Ok first for the ajax call. Replace your html.actionlinks with just buttons. put a class on them that you can use as a selector. Make sure you have jquery referenced on your page and add this to your scripts section

$(document).ready(function(){
    $('.btnDelete').on('click', function(){
        var tr = $(this).closest('tr');
        $.ajax({
            url: '@(Url.Action("Delete", "OtherDoctorDelete"))',
            type: 'post',
            data: {
                Id: $(this).closest('tr').attr('id') //should be able to simplify this to tr.attr('id') but this will work
            },
            success: function (result) {
                //can just do a tr.remove but the link I posted above adds some style to it
                tr.css("background-color","#FF3700");
                tr.fadeOut(400, function(){
                    tr.remove();
                });
            }
        });
    });
});

On your controller make sure that your delete method has the attribute httppost on it ([HttpPost] on the line above) and that it has an input parameter of Id. Whatever you call it make sure what is defined in the ajax call matched exactly. Then return json success or error depending on the result of the query (Asp.Net MVC3, returning success JsonResult). You may also want to put an "Are you sure" on this How to implement "confirmation" dialog in Jquery UI dialog? let me know if you have any questions.

Community
  • 1
  • 1
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • couple things. Url.Action takes action and controller. From what you have your action is Delete and your controller is OtherDoctorDelete. Second, by default routing takes an id parameter in the url. If you want to pass 2 parameters I wouldn't include them in the url.action like you have but put both parameters in the data section. Let me know if you have any questions – Matt Bodily Nov 19 '13 at 13:49