1

I have my JS function:

function LoadJs(hostApp) {
    $.ajax({
        type: "POST",
        url: '@Url.Action("LoadJs", "Home")',
        data: { "hostApp": hostApp },
        success: function (Js) {
            Response.Write(Js)   
        }
    });
}

When I call the function in my Summary.vbhtml file, I would like to pass it my hostApp object and then have the Javascript pass the object to a controller action.

Here is my script tag calling the LoadJs function:

<script type="text/javascript">    
    LoadJs( @(model)); 
</script>

I've tried '@model', '@(model)', and @modelbut nothing seems to pass it correctly. When I pass it the first two ways, it passes a string, which is not what I want and if I try the last way or the way in the example then it turned @model into the correct object, but it says it is undefined.

1 Answer 1

4

You can use

<script type="text/javascript">    
    LoadJs(@(Html.Raw(new JavaScriptSerializer().Serialize(model))));
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

My program is now giving a 500 error No parameterless constructor defined for this object. I have made my object class seriable and I have a parameterless constructor. Do you know why that would be happening?
And in the call stack of the exception, does it mark this line I posted or an other one?
It is being called on the @Url.Action in my js function
I know this isn't related to the question, and you're answer is working it seems. If you can answer if that would be a huge help though.
What if you put there '@Html.ActionLink("LoadJs", "Home")', does it work? EDIT: No, it won't work.
|

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.