Solved

mvc jquery Json style

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 42
ASP.NET 5 Templates 2 65
Need a starter for ETL protocol? 4 39
Get Client IP on RDS - VB.NET 15 25
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now