Solved

mvc jquery Json style

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

21 Experts available now in Live!

Get 1:1 Help Now