Async loaded partial view scripts initialization

On my control panel web page in ASP.NET MVC 3 site, a user can see his existing projects (its a site for designer portfolio), and can press a button to add a new project to the library.

A markup and the javascript dependencies for this particular "project adding functionality" are referenced in a partial view razor code.

Whenever a user click a button on a page, I retrieve the contents of the partial view and place them on the bottom of the page and call jQuery UI .dialog on a generated div to display a project editing/adding popup.

But how can I invoke a logic to initialize validation and other logic (click handlers for example) that is referencing elements in this partial view content? (Due to this I can't just initialize everything at document ready)

Do I just hardcode some function in a linked js (for example initProjectEditing()) and call it from the code that initializes the popup, or is there a more elegant way of doing it?

Answers


IMHO, the good way is to use Jquery widget . This thing allows you to define logic for your component in 1 place and then reuse it whenever you need.

  • You can create "add new project" Jquery widget.

  • Define all functionality concerning "add new project" in this widget (validation, event handlers, etc.)

  • Reference all javascripts needed for "add new project" directly from main view and not in partial view as you do now. You can create spetial partial view with such references if you need to reuse it in different places.

  • After your "add new project" partial view was loaded and dialog was shown, just init "add new project" widget with loaded html.


Need Your Help

Difference between behavior and -ms-behavior

html css components behavior

I don't understand the difference between behavior and -ms-behavior. I thought behavior was already only for Microsoft's Internet Explorer, so why is there a vendor prefix version?

why SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index and toString

sql sql-server user-input sqlexception jdbc-odbc

I have the following program in which the user must give the order code so to get the data of the client and his order. I have made a view in my database named Catalog which includes all the data n...