Question

So I am very new to Javascript and mvc.

I have been working on a backend service to pass back and forth very large objects between a mongodb and a javascript front end interface that will allow a user to populate those objects. The problem is my backend objects are several hundred lines of code long and I don't want to make a mistake copying them over into javascript objects because I am very new to javascript. Is there a way to convert a c# object class to a javascript class in order to create a model for an mvc framework? Is there some part of this structure I don't understand?

Was it helpful?

Solution

You should create a model server-side, corresponding to your "very large object".
Then, you can pass it to your JS from your controller using :

public ActionResult YourMethod(string param1)
{
    //get your object

    return Json(yourObject, JsonRequestBehavior.AllowGet);
}

You can call this method asynchronously from your JS thanks to ajax call :

$.ajax({
   url         :   "@Url.Action("YourMethod", "YourController")",
   contentType :   "application/json; charset=utf-8",
   dataType    :   "json",
   type        :   "POST",
   data        :   JSON.stringify({param1: "test"})
})

OTHER TIPS

Have a look at scriptsharp

It compiles c# to javascript. If you organize your code in a proper way you'll get classes from c# 1:1 in JS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top