Solved

mvc jquery Json style

Posted on 2013-11-07
4
438 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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