0

Is there a way to pass a HashTable loaded with key/values to a page View in a way I can then use it via javascript/Jquery ?

Example: I have HashTable with

key = Car Model (206 XT), Value = "PEUGEOT", etc...............

So in the view I have a combo with all car models and near that, I have a textbox automatically populated with the manufacturer taken from javascript

Is there a way to do so ??

(I'm really noob in MVC 3 :()

2 Answers 2

1

There are different way to solve this problem:

if you wan't to get this list dynamicaly with out reloading you can use a json request (examples).

Javascript:

 $.ajax({
       type: "POST",
       url: "/controler/action",
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       data: jsonData,
       success: function (result){

         for(var i=0; i<result.length; i++) {
        var value = result[i];
        alert(i =") "+value);
    }
       }

Controler (c#):

  [HttpPost]
   public JsonResult Action()
   {
      return Json(YourTable.ToArray()); 
   }

Furthermore you can access every html-element from javascript. So you can put the content in all kinds of html-elements.

Sign up to request clarification or add additional context in comments.

Comments

0

You can serialize most C# objects using the following method in the view:

<script type="text/javascript">
    var hashtable = @Html.Raw(Json.Encode(Model.YourHashtable)
</script>

If you have some Tree like data structures with parent/child relationships you might run into issues as the Encoder can't solve circular relationships.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.