pavan
pavan

Reputation: 34

JSon in MVC4 using javascript

I am new to JavaScript.... I have data in database...... I get it by using linQ as follows

 public List<SelectListItem> getTokens()
    {
        var tokens = from T in db.tokens select T;
        List<SelectListItem> items = new List<SelectListItem>();
        foreach (var t in tokens)
        {
          items.Add(new SelectListItem { Value = t.id.ToString(), Text = t.tname });
        }

        return items.ToList<SelectListItem>;
 }

Or

 public string getTokens()
 {
  var tokens = from T in db.tokens select T;
  string s = "[";
        foreach (var t in tokens)
        {
            s += "{ id:" + t.id.ToString() + ", name: " + t.tname + "},";
        }
        s += "]";
        return s;
}

i want to pass that string/List to my JS function something like this...

$(document).ready(function() {
        $("#demo-theme").tokenInput([
            { id: 7, name: "Ruby" },
            { id: 11, name: "Python" },
            { id: 13, name: "JavaScript" },
            { id: 17, name: "ActionScript" },
            { id: 19, name: "Scheme" },
            { id: 23, name: "Lisp" },
            { id: 29, name: "C#" },
            { id: 31, name: "Fortran" },
            { id: 37, name: "Visual Basic" },
            { id: 41, name: "C" },
            { id: 43, name: "C++" },
            { id: 47, name: "Java" }],
            {theme: "ab"
        });
    });

I need to replace the first list of items with my string/list... Or any other way to pass....

Upvotes: 0

Views: 154

Answers (2)

Dan Saltmer
Dan Saltmer

Reputation: 2165

You're almost there with your second one, you just need to convert it to a JSON response and there is no need to try to serialise it yourself.

public JsonResult getTokens()
{
    return Json(db.tokens.ToList(), JsonRequestBehavior.AllowGet);
}

Then this will return from an AJAX request , exactly what you want to pass into that javascript method (assuming a token has no extra properties you want to strip out).

Update: Jquery Ajax Documentation can be found at: https://api.jquery.com/jQuery.ajax/

However, let me offer you a simple alternative in case you are just trying to get an object into JavaScript and it doesn't need/want to be asynchronous. You can simply update your MVC ViewModel with a field which lists all your tokens and pass this into the script in your view.

public ActionResult YourExistingAction()
{
    YourExistingViewModel model = new YourExistingViewModel();
    model.Tokens = db.tokens.ToList();
    return View(model);
}

Then with your view:

@model YourExistingViewModel

<script>
    var tokens = @Html.Raw(Json.Encode(Model.Tokens));
    $(function() {
        $("#demo-theme").tokenInput(tokens, { theme: "ab" });
    });
</script>

Upvotes: 1

David
David

Reputation: 15350

public JsonResult getTokens()
{
    var tokens = from T in db.tokens select T;
    var items = tokens.Select(t =>
        new SelectListItem { Value = t.id.ToString(), Text = t.tname });

    return Json(items, JsonRequestBehavior.AllowGet);
}

Upvotes: 0

Related Questions