1

I want build a MVC Custom Control.

Now I have a problem.

My control has a file of js and a file of css.

I only want the page which used the control load the js and the css

and if i use this control maney times the js and css load once.

I know we can do like this in web form: ClientScript.RegisterClientScriptResource(typeof(..), "Assembly.MyScript.js");

but how can i do this in mvc??

there are two problem:

1.I want build a control to use in my project just like this @html.mycontrol() and the person who use it do not care any other(do not care if js or css load)

2.this control may use in other project.so i do not want the the other project know how to get the controls js or css(i Write this control in class liberary)

2 Answers 2

0

You control your includes through BundleConfig in MVC.

Normally, all bundles are loaded in the _Layout.cshtml document, like this:

@Scripts.Render("~/bundles/jquery")
@Styles.Render("~/Content/css")

You can make a custom bundle in the BundleConfig.cs file like this:

            bundles.Add(new ScriptBundle("~/bundles/nameOfBundle").Include(
                    "~/pathToFile"));

And then you just load the bundle in the file you need them in, instead of in the Layout.cshtml file.

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

2 Comments

I am sorry to answer so late. my problem is following:1.I want build a control to use in my project just like this @html.mycontrol() and the person who use it do not care any other(do not care if js or css load) 2.this control may use in other project.so i do not want the the other project know how to get the controls js or css
I think you might be refering to custom HTML helpers. You can read this article about exactly that:blogs.technet.com/b/sateesh-arveti/archive/2013/09/06/…
0

Return Javascript from MVC COntroller

MSDN Documentation

Example:

[OutputCache(Duration = 999999)]
public virtual JavaScriptResult Global()
{
var script = string.Format(@"
MaxNotificaitonsToShow = {0};
ItemsPerPage = 25;",
GlobalSettings.MaxNotificaitonsToShow);
return JavaScript(script);
}

3 Comments

I am sorry to answer so late. my problem is following:1.I want build a control to use in my project just like this @html.mycontrol() and the person who use it do not care any other(do not care if js or css load) 2.this control may use in other project.so i do not want the the other project know how to get the controls js or css
first: I want use razor code like this @html.mycontrol() ,when I use the code ,the js and css load automatic, do not need any other code. second:I want this control write in class liberary ,and can use in anywhere.
first question the css and js only load when i use @html.mycontrol() and when i use this @html.mycontrol() more than one time ,the share css and js only load once. seconde question how to use this control in any other project

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.