• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1097
  • Last Modified:

MVC 5 AJAX Post DIV Form Seriaiaze to Action Model Always Null?

I cannot post Div values to an MVC Action model, always null. I've tried many article examples and still get nulls. Capturing the .serialize in an alert looks like the pic below. I'm not understanding how an MVC Model can accept a serialize object. It looks like a simple querystring...

Serialized Div Data
Here's my code so far:

$.ajax({
        url: 'Posts/Save',
        data: $("#form1 :input,text,textarea,select,hidden").serialize(),
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        cache: false,
        success: function (result) {
            alert(result);
        },
        error: function (error) {
            alert("ERROR: " + JSON.stringify(error));
        }

    });

[Action]
 public JsonResult Save(PostUpdateViewModel model)
 {
 }
0
WorknHardr
Asked:
WorknHardr
  • 2
1 Solution
 
Gary DavisDir Internet SvcsCommented:
In the POST, the data must not be set correctly. You can look at what the network sends using HttpWatch or Developer Tools in the browser (F12 - network tab). Another thing I'd do would be to use the script debugger in the Dev Tools and set a breakpoint at the start of the Ajax call and view the contents of the expression:

$("#form1 :input,text,textarea,select,hidden").serialize()

and verify it is correctly serializing the expected form field contents.

Also, you mention div contents but the Ajax data is a textarea apparently. I am not sure if your question is stated correctly or the statement is wrong (or neither).

Gary Davis
0
 
WorknHardrAuthor Commented:
I found another post where they added these properties to Ajax and it started working:

   $.ajax({
         url: 'Posts/Save',
         data: $("#form1 :input,text,textarea,select,hidden").serialize(),
         type: 'POST',
         cache: false,
         contentType: false,
         processData: false,

         success: function (result) {
             alert(result);
         },
         error: function (error) {
             alert("ERROR: " + JSON.stringify(error));
         }
     });
0
 
WorknHardrAuthor Commented:
Thanks for your help. I found the answer elsewhere...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now