Wednesday 15 January 2014

asp.net mvc - Render scripts for use in Partial View? -


I have seen many sources that it is wrong / wrong to insert scripts in partial scenes, but it is a big Raises the question ...

Are you going to run a script that talks with partial scenes? I have tried to use @section scripts {} or something like this, but it does not work exactly at all. The scripts are just fine in this section, but they are not provided when loaded in a partial full view.

Also, I can not present the script to partial views on my entire scene because the script pulls the value from model , which is only provided on partial view, because It's a piece of puzzle that actually works with data.

I hope this sound is very complex ... I have elements of my partial view and model How to use with > Can you present the script efficiently, efficiently, and correctly?

@keyCrumbs I will not give you a direct answer but you have to do some analysis.
The biggest problems in you are the replication of the call script partial view code

Think that you will use AJAX to get a partial view and you will continue this for a while. In each call you will download the script code, and you will put it in html. Every time you reset the script block to html, the functions are reset, the variables are reset. This can be a big problem depending on your JS code.

The other point is the size of the response, you can say, but JS is a small size, and I would say to increase this call for each call, for the user and connecting to the next user Multiplied next to

So in the end, there is a solution for you, in this case: Create a function in the page and call the function in a partial view like this:

Your page: Page .cshtml

  & lt; Script type = "text / javascript" & gt; Function myPartialView_Load () {$ ("birth"). Datepicker (); . $ ("Phone") mask ("000-0000"); } & Lt; / Script & gt; & Lt ;! - Some other code here - & gt; & Lt; Div & gt; & Lt ;! - Or you can call in an AJAX or other way ... - & gt; @html.action ("MyActionPerial electricity") & lt; / Div & gt;   

Your partial view: MyPartialView.cshtml

  & lt; Script type = "text / javascript" & gt; $ (Function () {myPartialView_Load ();}); & Lt; / Script & gt; & Lt; Form & gt; & Lt; Input type = "text" name = "birth" id = "birth" /> & Lt; Input type = "text" name = "phone" id = "phone" /> & Lt; / Form & gt;   

So as you see that the problem is not partly in JS, but the way you do it is a "why" behind all the rules of good practice, If you think that "why" you can determine that governance is a help and there is no burden. And break it or not.

One way said, I have not given you the last answer, but there is something that is about you. You can use other ways to contend with the problem.

No comments:

Post a Comment