Solved

mvc jquery Json style

Posted on 2013-11-07
4
441 Views
Last Modified: 2013-11-13
The first static example everything returns corrrectly when I am using my dynamic example the Json data returns as one string with commas after each value.
 
public JsonResult GetSite()

{

   var sites = new[]

    {

new {Nome = "site 1" },    new {Nome = "site 3" },new {Nome = "site 4" },

    };

     return Json(sites, JsonRequestBehavior.AllowGet);

    } 

Open in new window

Now I have my Data Dynamically I am pulling from a model.
  public JsonResult mystore()
        {
                   List<string> ChainResultList = ListOfChain();
               var sites = new[]
    {
        new {Nome = ChainResultList},
       
    };
           return Json(sites, JsonRequestBehavior.AllowGet);

        }

Open in new window

But this returns everything as one long string. Or commas after the value.
I am assuming that new {Nome = ChainResultList}, needs to be with the new SelectList in order to appear correctly.
 new SelectList(ChainResultList)  $(function() {
                    $.getJSON('@Url.Action("GetSite", "Home")', function(result) {
                        var ddl = $('#MyList');
                   ddl.empty();
                   $(result).each(function (i, item) {
                       alert(this.Nome);
                       ddl.append(
                           $('<option/>', {
                               value: this.Id

                           }).html(this.Nome)

                       );
                   });
               });
           });
    });    
.

Open in new window

0
Comment
Question by:sevensnake77
[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
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39632210
Your two examples are not equivalent. The "static" example assigns strings to Nome; the "dynamic" example assigns List<string>s to Nome. Which is correct?
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39632251
The static is just an example. but I am actually using the dynamic. The dynamic one I like to pass to my json data but it is not coming out correct. string with commas. How can I pass it like the static.?
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39638929
Ok I found out i need to set up a datatext field and a datavalue field within the controller that is my problem.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 39646296
tks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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