Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. MVC 4.0 and JQuery .ajax

MVC 4.0 and JQuery .ajax

Scheduled Pinned Locked Moved ASP.NET
helpjavascriptasp-netdebuggingjson
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    I'm trying to do a post back to a controller with $.ajax and all I get in the controller is null for my object. Here's my setup for the class:

    public class ApplicationLinkViewModel : MappableModel
    {
    public string ApplicationLinkId { get; set; }

        public string ApplicationId { get; set; }
    
        public string DisplayText { get; set; }
    
        public string Link { get; set; }
    }
    

    And for the controller:

    [HttpPost]
    public JsonResult SaveLink(ApplicationLinkViewModel link)
    {
    var responseModel = new ResponseModel();
    return this.Json(responseModel);
    }

    As you can see I'm doing nothing in the controller right now, I just have two lines so I can set a breakpoint and check out the link variable which is always null. The JQuery function is :

    $.ajax({
    type: "POST",
    cache: false,
    dataType: 'json',
    url: "Item/SaveLink",
    data: JSON.stringify({ ApplicationId : "1", DisplayText: "test", Link : "linked", ApplicationLinkId: "2" }),
    contentType: 'application/json; charset=utf-8',
    success: function(data) {
    if (data.Success) {
    window.searchApplications();
    window.showDetails(applicationId);
    }
    },
    error: function(data) {
    alert("Error occurred : \n\n" + data.Message);
    }
    });

    I've tried several other approaches, and nothing I do seems to work, the object passed into my controlled is always null. Does anyone have any ideas? For what it is worth I am (stuck) using IE8. EDIT: SOLVED I'm going to go drown myself now. The problem was the name of my parameter "link". It must be reserved or something even though the code never complained. At any rate I changed my Controller to this and it works now:

    public JsonResult SaveLink(ApplicationLinkViewModel linkViewModel

    A 1 Reply Last reply
    0
    • A AnalogNerd

      I'm trying to do a post back to a controller with $.ajax and all I get in the controller is null for my object. Here's my setup for the class:

      public class ApplicationLinkViewModel : MappableModel
      {
      public string ApplicationLinkId { get; set; }

          public string ApplicationId { get; set; }
      
          public string DisplayText { get; set; }
      
          public string Link { get; set; }
      }
      

      And for the controller:

      [HttpPost]
      public JsonResult SaveLink(ApplicationLinkViewModel link)
      {
      var responseModel = new ResponseModel();
      return this.Json(responseModel);
      }

      As you can see I'm doing nothing in the controller right now, I just have two lines so I can set a breakpoint and check out the link variable which is always null. The JQuery function is :

      $.ajax({
      type: "POST",
      cache: false,
      dataType: 'json',
      url: "Item/SaveLink",
      data: JSON.stringify({ ApplicationId : "1", DisplayText: "test", Link : "linked", ApplicationLinkId: "2" }),
      contentType: 'application/json; charset=utf-8',
      success: function(data) {
      if (data.Success) {
      window.searchApplications();
      window.showDetails(applicationId);
      }
      },
      error: function(data) {
      alert("Error occurred : \n\n" + data.Message);
      }
      });

      I've tried several other approaches, and nothing I do seems to work, the object passed into my controlled is always null. Does anyone have any ideas? For what it is worth I am (stuck) using IE8. EDIT: SOLVED I'm going to go drown myself now. The problem was the name of my parameter "link". It must be reserved or something even though the code never complained. At any rate I changed my Controller to this and it works now:

      public JsonResult SaveLink(ApplicationLinkViewModel linkViewModel

      A Offline
      A Offline
      AnalogNerd
      wrote on last edited by
      #2

      For what it is worth changing the definition of my controller method to the following works.

      public JsonResult SaveLink(string ApplicationLinkId, string ApplicationId, string DisplayText, string Link)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups