0

I have a create,edit,delete application On my Index view i have Button for Edit. by clicking this button it should open pop up in which all data should be displayed for editing.

To achieve this i passed ID of that row which is getting Edited. see code below :

  <td>
    <button type="button" onclick="EditPopup(@item.Id)">Edit</button>
    </td>

here i am passing ID to my EditPopup javascript method. see the method below :

<script type="text/javascript">
$(document).ready(function () {
    $("#EditDialog").dialog({
        autoOpen: false,
        title: 'Title',
        width: 'auto',
        height: 'auto',
        modal: true
    });
});

function EditPopup(Get_Id) {
   alert(Get_Id) // I am getting correct ID here.
    $.ajax({

        method: "POST",
        url: '@Url.Action("Edit","Home")',
        dataType: 'json',
        cache: false,
        data:{Get_Id}, // tried : {id:Get_Id} , {id:"Get_Id"} not working
        success: function (data) {

            $('#EditDialog').html(data);
        }

    });
    $("#EditDialog").dialog("open");
}</script>

I am sending value of ID to my Controller method Edit thats why i am using Post method in ajax call. Edit is name of method and Home is name of controller.

HomeController Edit methods

 [HttpPost]
    public JsonResult Edit(int? id)
    {

        FloorFactor floorFactor = db.FloorFactors.Find(id);

        return Json(floorFactor, JsonRequestBehavior.AllowGet);
    }

    // POST: 
    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {

        FloorFactor floorFactor = db.FloorFactors.Find(id);

        return View(floorFactor);
    }

in few examples i saw that in ajax call they usually use json result method. so that is the reason i also used json result method.

finally Code which is in my index view where i will show pop up result.

<div id="EditDialog" style="display:none;">
<label> Floor Factor </label>
<br />
<label> Effective From :</label>

So the Whole scenario is :

I send id value on button click event to javascript.
on javascript i make a call to my controller method to get data.
those should pass in EditDialog box of div.
on div block it should display in pop up.

Current output :

enter image description here

I also want to understand how url field works in ajax call.
if i am getting multiple column results as output of that url how can i collect all output in Data part of ajax call.
please also explain on success what parameters i can pass in function.

Thank you for explanation and help.

Edit : It shows no error on console tab. Html Screen of browser when i click on button Last Part of Script tab as shown in this script tab i think it is sending a request as it generates request Id.

Harshil Shah
  • 203
  • 1
  • 16
  • try change `data:{Get_Id}`, to `data:{id : Get_Id}`, – Carsten Løvbo Andersen May 25 '16 at 10:02
  • Move $("#EditDialog").dialog("open"); inside the success callback otherwise it will get called before response returned . – Ajinder Singh May 25 '16 at 10:05
  • Dont do `{id:"Get_Id"}` but `{id:Get_Id}` Remove the "" – Carsten Løvbo Andersen May 25 '16 at 10:07
  • Also i don't think $('#EditDialog').html(data); will work. You can collect output property value as data.property_name and then assign to desired html element. – Ajinder Singh May 25 '16 at 10:10
  • @CarstenLøvboAndersen it's not working The output is same. Can you explain what difference it will make ? i tried both with quotes n without AjinderSingh : now it is not opening pop up. that means success method is not getting called. why is that ? – Harshil Shah May 25 '16 at 10:12
  • Check the status of the actual request manually by examining your console (F12 in most browsers). Was the request successful? Did the payload have the proper information? – Michael.Lumley May 25 '16 at 10:17
  • @HarshilShah - Check if request is successful or not ? Browser console if there is any error ? – Ajinder Singh May 25 '16 at 10:22
  • 1
    @HarshilShah - Check the browser Network tab and see what response it is returning . – Ajinder Singh May 25 '16 at 10:35
  • In response body it shows 'System.Reflection.AmbiguousMatchException in one of many lines. The current request for action 'Edit' on controller type 'HomeController' is ambiguous between the following action methods:
    System.Web.Mvc.JsonResult Edit(System.Nullable`1[System.Int32])'
    – Harshil Shah May 25 '16 at 10:42
  • 1
    @HarshilShah Comment out one of two Edit action method . If you want to return object then keep the JsonResult Action and if returning View then keep the other one. – Ajinder Singh May 25 '16 at 10:48
  • Thanks i commented one of my Post method and now it is returning me json string value. now i just want to get different values from that string. – Harshil Shah May 25 '16 at 10:49
  • 1
    @HarshilShah - Great . you can access the property value using data.property_name and assign it to html element . At last call the dialog show function. – Ajinder Singh May 25 '16 at 10:50
  • can you give me an example like "Effective from" is name of a property which i am getting in json string value. but when i try to write data.Effective from" it is not showing me in intellisense. so how to write it ? – Harshil Shah May 25 '16 at 10:58
  • Just Post your FloorFactor Entity Class Definition with question. then i will be able to tell you how exactly you can access the Effective Form – Ajinder Singh May 25 '16 at 10:59
  • @HarshilShah - Try data.Effective_Form_Property_Name in Floor Factor class name. Also check my answer if it helps. – Ajinder Singh May 25 '16 at 11:06

2 Answers2

1

Try the below changes

Action Code :

[HttpPost]
    public JsonResult Edit(int? id)
    {

        FloorFactor floorFactor = db.FloorFactors.Find(id);

        return Json(floorFactor, JsonRequestBehavior.AllowGet);
    }

View Changes

<div id="EditDialog" style="display:none;">
<label> Floor Factor </label> <span id="floorFactor"></span>
<br />
<label> Effective From :</label> <span id="effectiveFrom"></span>

Success method changes

      if(data)
      {
//  GET VALUES
         var floorFactor = data.Property_Having_FloorFactor;
         var effectiveFrom = data.Property_Having_EffectiveFrom; 
// ASSIGN VALUES
         $('#floorFactor').text(floorFactor);
         $('#effectiveFrom ').text(effectiveFrom );
// OPEN DIALOG
         $("#EditDialog").dialog("open");
      }

Hope it will work for you.

Ajinder Singh
  • 532
  • 2
  • 8
  • actually effective from is a date and we are sending it as a text. so i think because of that it is not providing correct result. how to avoid that ? – Harshil Shah May 25 '16 at 11:33
  • I asked other questions also please answer them too. n also explain why we are using post method here. Is it because we are sending ID to our controller method ? If yes i think in create method call i will again use post method only.. ? – Harshil Shah May 25 '16 at 11:41
  • @HarshilShah - you can use the GET method here . In GET request case id passed as query string and POST it's passed in body. – Ajinder Singh May 25 '16 at 11:56
  • okay. and can you answer other questions also please ? and "Effective from" is date type so any solution for that ? – Harshil Shah May 25 '16 at 12:10
  • @HarshilShah - Either format the EffectiveForm on server side before returning response or you can format it on client side as well. Give below thread a look http://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format/726417#726417 – Ajinder Singh May 25 '16 at 13:13
  • That question helped me. now i am showing data on label floorfactor using `` but as it is edit button i need a text box where i can show current data and edit them. i tried `` and used it's id in javascript but it doesn't showing – Harshil Shah May 26 '16 at 07:19
  • 1
    @HarshilShah, If its a textbox then you need to use `.val()` to set it - `$('#floorFactor').val(floorFactor);` where the textbox has `id="floorFactor"` –  May 26 '16 at 07:36
  • Thanks it worked !!. please add that fiddle it will help me to learn more on this – Harshil Shah May 26 '16 at 07:44
  • @HarshilShah - Just create a new thread if you have more questions . – Ajinder Singh May 26 '16 at 09:02
0

Change the Controller as

[HttpGet]
public JsonResult Edit(int? id)
{

    FloorFactor floorFactor = db.FloorFactors.Find(id);

    return Json(floorFactor, JsonRequestBehavior.AllowGet);
}

// POST: 
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{

    FloorFactor floorFactor = db.FloorFactors.Find(id);

    return View(floorFactor);
}

Change the 'ajax' as

$.ajax({
    method: "GET",
    url: '@Url.Action("Edit","Home")',
    dataType: 'json',
    cache: false,
    data: {id:Get_Id},
    success: function (data) {
        $('#EditDialog').html(data);
    }
});

Note: Code is untested. It should have work for you.

Mahedi Sabuj
  • 2,894
  • 3
  • 14
  • 27