1

In ASP.NET, I would have an ascx control, and it would call Scriptmanager.RegisterClientScriptBlock in its own Load() method.

I'd have a block of javascript like

var myObject = CreateNewMyObject(param, param, param...);

which would be a method in my javascript library to go off and create the control on the page.

However in MVC I have never done this before so I am unsure what to do.

I can call a method such as @(this.Html.RenderMyObject(param, param)) which will return fully rendered html, but I want to do it as above - the javascript function renders the html.

Should I return var myObject = CreateNewMyObject(param, param, param...); from a method like this, or is there a much nicer way?

1 Answer 1

1

I think the idea with MVC, especially with Razor, is that you use Javascript as you want to. If I understand your question correctly, you could call your javascript function directly in the onLoad javascript function on the Razor page.

<script type="text/javascript">
$(function () {
    myMethodCall();
</script>

However, if the only thing your javascript does is paint up a control, I would recommend creating a @HTML Helper like you mentioned in your question.

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

5 Comments

Yeah I think my question is a bit badly worded. If we assume my control renders <span>hello [name]</span> my html helper would be @(renderHello("bob")); which would inject the function() call as you say, but it needs to pass parameters, so I would think it'd render something like <span id='someId'></span> <script>myMethodCall(someId, 'bob');</script> but that seems somewhat tacky.
Forgive me, because I don't know as much about javascript as I would like to yet. But the way I imagine your problem follows a slight shift in your approach. Rather than having @(renderHello("bob")) you simply have the span in your html <span id='someId'></span>. Then your javascript would have a myMethodCall(someId, 'bob');. Which is great, and not tacky iirc. Then, because you probably want real data, you use the model to pass in your data to the javascript. Something along the lines of $(function () { @foreach (var t in model) myMethodCall(someId, t.Name) }) (Likely invalid syntax)
Thinking a little bit more, your javascript would likely create the span and then append it to another element. Again, I hope this helps and isn't completely the wrong approach.
Yup it makes sense, I think I want to do it from a helper though so that you don't need to understand the internal workings (you have to know to create a span, and give it a specific class, and make the specific javascript call with lots of injection from the model.). With a helper I'll have full intellisense, it won't compile the page if there's an error, and it's easier to refactor. Plus I think I'll make the javascript call a controller method to get the data so I won't need to pass much in =) I'll get the html to do the injection bit. Cheers for the advice!
Cheers for giving my first checkbox!

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.