1

I am calling jQuery dialog from @Html.ActionLink But I am confused to pass action parameters to jQuery dialog. How to pass them CommunicationLocation and CommunicationType to jQuery dialog?

enter image description here

 @Html.ActionLink("Delete", "DeleteEmail", null, new { CommunicationLocation 
    = commemail.CommunicationLocation, CommunicationType = "email" }, 
    new { @class = "modalDialog btn btn-success" })

Dialog Div

<div id="dialog-confirm" title="Delete the item?">
        <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be deleted. Are you sure?</p>
    </div>

JS

     <script type="text/javascript">
 $(document).ready(function () {
            $("#dialog-confirm").dialog({
                autoOpen: false,
                modal: true,
                resizable: false,
                height: 180,
            });
            $('.modalDialog').click(function () {
                $("#dialog-confirm").dialog({
                    buttons: {
                        "Confirm": function () {
                            $.ajax({
                                url: 'customer/DeleteEmail',
                                type: 'Post',
                                data: //Need to pass parameters here,
                                success: function (result) {
                                    $('#DivEmailContainer').html(result);
                                }
                            });
                            $(this).dialog('close');
                        },
                        "Cancel": function () {

                            $(this).dialog("close");
                        }
                    }
                });
                $("#dialog-confirm").dialog("open");
                return false;
            });
        </script>

Controller

 [HttpPost]
    public PartialViewResult DeleteEmail(string CommunicationLocation,  string CommunicationType)
    {
          //Code here
    }
James123
  • 11,184
  • 66
  • 189
  • 343

2 Answers2

3

You could attach the values to the ActionLink with data- attributes.

Something like this:

CSHTML:

@Html.ActionLink("Delete", "DeleteEmail", null, 
new { @class = "modalDialog btn btn-success", data_location 
= commemail.CommunicationLocation, data_type= "email" })

JS:

$('.modalDialog').click(function() {
  //Declare this variable to track the clicked button, so we could get its
  //data attributes after dialog confirmation
  var $button = $(this);
  $("#dialog-confirm").dialog({
    buttons: {
      "Confirm": function() {
        $.ajax({
          url: 'customer/DeleteEmail',
          type: 'Post',
          data: {
              CommunicationLocation: $button.data('location'),  
              CommunicationType: $button.data('type')
            },
            success: function(result) {
            $('#DivEmailContainer').html(result);
          }
        });
        $(this).dialog('close');
      },
      "Cancel": function() {

        $(this).dialog("close");
      }
    }
  });
  $("#dialog-confirm").dialog("open");
  return false;
});
jwatts1980
  • 7,254
  • 2
  • 28
  • 44
Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
  • You need to double check that `Html.ActionLink` overload https://msdn.microsoft.com/en-us/library/dd492124(v=vs.118).aspx. As you have written it, `data_location` and `data_type` will be querystring values instead of `data-` attributes. The HTML attributes should go into last parameter object like `@Html.ActionLink("Delete", "DeleteEmail", null, new { @class = "modalDialog btn btn-success", data_location = commemail.CommunicationLocation, data_type= "email" }` – jwatts1980 Apr 14 '15 at 16:20
2

You could get the URL from the link. In the click action,

var url = $(this).attr("href");

To parse the URL and get the querystring parameters, I would recommend checking out this very popular SO post: How can I get query string values in JavaScript?

An alternative would be to create a script tag and write the values to JS variables:

<script>
    var CommunicationLocation = "@commemail.CommunicationLocation";
    var CommunicationType = "email";
</script>
//or...
<script>
    var CommunicationInfo = {
        "CommunicationLocation": "@commemail.CommunicationLocation",
        "CommunicationType": "email"
    };
</script>

From there you can use the variables in your JS script.

UPDATE

To handle rows of items you will need either an index value or some unique id.

<script>
    var infoArray = []; //create this before the rows are created

    //in the row code...
    infoArray.push({
        "rowID": ###, //<--Some unique row id would be needed as a way to 
                      //look up the correct item.
        "CommunicationLocation": "@commemail.CommunicationLocation",
        "CommunicationType": "email"
    });
</script>

Then when you create your action link, add the data-id attribute to the link (MVC3+):

 @Html.ActionLink("Delete", "DeleteEmail", null, new { CommunicationLocation 
    = commemail.CommunicationLocation, CommunicationType = "email" }, 
    new { @class = "modalDialog btn btn-success", data_rowid = "some id value" })

To clarify, data_rowid in the action link will translate to <a data-rowid=""> in MVC3+, which can be easily retrieved by jQuery like in the example below.

In your click action:

var rowid = $(this).data("rowid");
var CommunicationInfo = $.grep(function(n) { return n.rowID == rowid; })[0];
alert(CommunicationInfo.CommunicationLocation);
Community
  • 1
  • 1
jwatts1980
  • 7,254
  • 2
  • 28
  • 44
  • For alternative method, I will have each row has values so I cannot store values in variable because I don't know which row I clicked on – James123 Apr 14 '15 at 15:13
  • @James123 I have updated my answer to include a solution that I have used in numerous occasions for similar scenarios. Basically, I create an array of items with some lookup id, like an id from the DB or an index. As the rows are created the ID value is added to a `data-` attribute and a Javascript array is updated with the new item. In the JS event action, the id is retrieved to get the proper array item. – jwatts1980 Apr 14 '15 at 15:49