Solved

.NET MVC JSON to Web Service to JSON to Controller

Posted on 2012-03-23
7
444 Views
Last Modified: 2012-03-31
In the JavaScript, notice arg.d contains the List<Fixtures> object and its 13 values.I have a text box in my "/Shared/_Layout.cshtml" page (so, obviously, this appears on every page of the Website) and I am having a heck of a time tyring to accomplish the following.

I'm sure I'm missing something, but if anyone can help that'd be great.

So, the user inputs text into the text box for searching purposes. When the user clicks "enter", a JavaScript function is called that, in turn, calls a Web Service.

The Web Service takes the inputted text and gets the results in a List<Fixtures>.

Here is the JavaScript:
function submitSearch(text) {
    var params = "{'text':'" + text + "'}";

    $.ajax({
        type: "POST",
        url: "/Services/Search.asmx/SearchProducts",
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (arg) { searchSuccess(arg, text); },
        error: function () { alert("An Error Occured Retrieving the Data."); }
    });
}

Open in new window

Here is the Web Service:
[WebMethod]
public List<Fixtures> SearchProducts(string text)
{
    Session sh = SessionHelper.GetSessionObject();
    List<Fixtures> myFixtures = SearchLogic.GetSearchResults(sh.BrandObject, text);
    sh = null;

    if (myFixtures != null && myFixtures.Count > 0)
    {
        List<Fixtures> nonDupes = myFixtures.GroupBy(x => x.Name).Select(y => y.First()).ToList();
        if (nonDupes != null && nonDupes.Count > 0) myFixtures = nonDupes;
        nonDupes = null;
    }

    return myFixtures;
}

Open in new window

Then the "searchSuccess()" javascript function gets called. In that function, I want to pass the returned value from the Web Service (now stored in "arg.d") and send it to a Controller.

Here is the searchSuccess() function:
function searchSuccess(arg, text) {
    var params = "{'myFixtures':'" + arg.d + "'}";

    $.ajax({
        type: "POST",
        url: "/Products/Search",
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}

Open in new window

This is where the issue is happening.

In the attached image at the top of the page, you will notice that in the searchSuccess() function, arg.d does have the List<Fixtures> object and its 13 Fixtures objects that are in the List.

However, when I try to send that to the Controller, you'll notice the object gets sent over but the object is empty (see 2nd attached image (mvc2.gif) at the bottom of the page).

Here is the Controller:
public ActionResult Search(List<Fixtures> myFixtures)
{
    if (myFixtures != null && myFixtures.Count > 0) return View("Fixtures", myFixtures.ToPagedList(0, 1000));
    else return View();
}

Open in new window


Any idea why it won't pass the entire object through and how would I go about doing this?
mvc2.gif
0
Comment
Question by:infotechelg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37759605
arg.d is a JSON object
you're trying to embed it in a string with your code :
var params = "{'myFixtures':'" + arg.d + "'}";

Open in new window


It's not possible.
Two ways :
- first one :
Convert arg.d to a string like this : JSON.stringify( arg.d )
Please note JSON.Stringify is only available on "recent" browsers, you can embed this small json.js script in your page to let every browser able to use it.

- second way (look more appropriate for your current controller):
var params = { "myFixtures" : arg.d };

Open in new window

0
 

Author Comment

by:infotechelg
ID: 37766472
leakim:

Thanks for the reply. I tried the JSON.stringify(arg.d) suggestion and i'm still getting a count of "0" when the varabile gets to my controller. (I also included that .js file you linked to)

When I tried the 2nd way, "var params = { "myFixtures" : arg.d };", the controller didn't even get called.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37766773
could you confirm List<Fixtures> match with the array of object arg.d ?

could you post it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:leakim971
ID: 37766782
a JSON example (or a link to your page) and the Fixtures object
0
 

Author Comment

by:infotechelg
ID: 37766900
it does match. Notice the Web Service method is returning List<Fixtures> back to the JSON.
0
 

Accepted Solution

by:
infotechelg earned 0 total points
ID: 37768585
Well, i didn't find the solution, but I changed how I preformed the search. Instead of doint a web service call via JSON in my JavaScript, I just posted Form tags around the search box and had the controller handle the search directly.
0
 

Author Closing Comment

by:infotechelg
ID: 37790430
changed how I did it so I could get it to work.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question