Ok, so I am attempting to implement the typical image-upload capability in my website. (I never expected it to be so complicated!!!) I finally found an AWESOME nuget package (MvcFileUploader) that uses the jQuery-File-Upload plugin. It works awesome, but I am having a little trouble figuring out how to associate this with my entities.
It is quite obvious how to create an Image entity when the files are uploaded. But then, I have many different types of entities that I would like to associated with the pictures after they are uploaded (e.g. news articles, blog posts, classified ads, etc.).
The way I have it set up currently is that I have the file uploader on the right side of the page and a form for a news post on the left. The file uploader does its thing and uploads a file, creating Image entities in the process. The problem arises when I want to then associate those new Image entities with my News entity. (Keeping in mind that at the time the Image entities are created, no news entity has been created yet.)
The first solution that came to mind was use AJAX to stick a hidden field into the news item form to represent the new image entity id. However, unfortunately, I can't seem to figure out any good way to make this work since I don't have easy and understandable access to the MvcFileUploader code to try to make it do such a thing.... and it works so well right now I am frightened of screwing it up. :) (It is a minified .js file.) (If anybody knows how I could do that easily and cleanly without screwing up the file uploader plugin, I would love to hear about it.)
So, my next idea is that I could create another type of entity whose sole purpose is to connect the Image and the news item together. What I would do is create that new entity (call it XYZ) when I output the forms and put a hidden field in both the news item form and the image upload form (since the mvcfileuploader provides the capability to output hidden fields into the form to be uploaded with the file-upload request). These hidden fields would each contain the ID of the XYZ entity for this particular news article. When I created the image entity, I would put its ID in the XYZ entity to wait for the News Item to retrieve when it is created. I think this might work, but it feels extremely messy. I don't like the idea of having another entity just for this purpose, and after the News Item is created (or the user simply leaves the news item creation page) I would still have those entities in my database. I would have to go clean them out regularly or something.
I also thought about simply making the News Item when the Create page was outputted and outputting the News Item ID into both forms... but then I would have to forgo any sort of required fields and other constraints on the news item fields AND I would have empty news items if the user left the page before finishing. Messy too.
Somebody help please :).
I would probably separate the views. The workflow might be for example:
News
Create page without file uploaderNews
created in databaseNews
item with File uploader on right sideThat's with the idea in mind that a News
item makes sense without uploaded files, but an uploaded file doesn't make sense without News item. I don't know if that is a feasible assumption for your project.
For the user you could indicate on the Create page that he can upload files in a second step, like: "Please enter the information for this news item. Note: You can upload related files after the News
has been created." Or so...