Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data with jquery to an MVC controller

I have an ASP.NET MVC3 app and when the user clicks on my anchor tag, I want to send 3 pieces of data to an action:

 <a onclick='editDescription(<#= DocID,FileName,Description #>)'></a>

This is the javascript to call my action:

   function editDescription(docId,fileName,description) {
     var url = "@Url.Content("~/OrderDetail/_EditDescription/")" + docId+'/'+
     fileName + '/' + description;
    //do the rest}

My action:

  public ActionResult _EditDescription(string id,string filename, string descritpion)

The pieces im concerned about are FileName and Description because these can be loooooong and i dont want a url to appear like so:

 http://localhost/OrderDetail/_EditDescription/123/some long filename.pdf/this is a    long description for the name

How can i send across my data to my action without having to send it like a query string? Thanks

like image 833
BoundForGlory Avatar asked Jun 13 '12 13:06

BoundForGlory


People also ask

How pass JSON data to AJAX to controller?

Create target "JSON object Mapper" object class file according to the business requirements. Create a "Controllers\HomeController. cs" file with default Index method and GetData(...) method with string type input query parameters for Ajax call with following lines of code i.e.


2 Answers

You can use the jQuery $.ajax method:

<div id="what-I-want-updated">

  <input id="whatever-the-id-is" type="text" value="@Model.ID" />
  <br /> 
  <input id="whatever-the-filename" type="text" value="@Model.Filename" />
  <br />
  <input id="whatever-the-description" type="text" value="@Model.Description" />
  <br />
  <button id="whatIsClicked">Update!</button>

</div> <!-- /#what-I-want-updated -->

<script>

    // You're probably clicking something to initiate update 
    var $whatIsClicked = $('#whatIsClicked');

    // .live persists on the page even after other ajax calls
    // So when the thing is clicked
    $whatIsClicked.live('click', function() {

       // Grab the information needed to update
       var theId = $('#whatever-the-id-is').val(); //Or it could be .text()
       var theFilename = $('#whatever-the-filename').val();
       var theDescript = $('#whatever-the-description').val();

       // Let's edit the description!
       $.ajax({
         type: "POST",
         url: "OrderDetail/_EditDescription", // the method we are calling
         contentType: "application/json; charset=utf-8",
         data: {id: theId, filename: theFilename, description: theDescript},
         dataType: "json",
         success: function (result) {
             alert('Yay! It worked!');
             // Or if you are returning something
             alert('I returned... ' + result.WhateverIsReturning);                    
         },
         error: function (result) {
             alert('Oh no :(');
         }
     });
    });
</script>

Even though it will still work, make sure you change your Controller method to:

[HttpPost]
public ActionResult _EditDescription(string id, string filename, string descritpion)
like image 131
David East Avatar answered Sep 22 '22 17:09

David East


You can do a full post of the form if you like either through ajax $.post or by having an action with [HttpPost] attribute.

like image 31
AD.Net Avatar answered Sep 24 '22 17:09

AD.Net