views:

836

answers:

3

Hey

I'm looking into adding a jquery modal popup to my mvc project. I have been looking at http://www.weirdlover.com/2010/05/20/mvc-render-partial-modal-pop-up-via-jquery-and-colorbox-demo/ posting.

My scenario: I have a page with a few input fields (textbox, checkbox). I want to have a link that will open a modal popup to select an image. I have a control that will display all images and allow the user to select an image. I understand how to open the modal popup following the site listed above.

What I'm not understanding right now is how to return the selected image name (myimage.jpg) to my original pages view and set it to a property on the model with all the other model changes kept intact.

What I have currently: MyFormController > ActionMethod:ImageSelected(ImageSelectorModel model)

If it is valid, how do I grab my "MyFormModel" (with all other changes), set MyFormModel.ImageName = model.ImageName and call View(myFormModel).

Thanks for your help

Kevin

A: 

Hey Kevin,

If the user is uploading an image, I recommend that you check out AJAX Upload: http://valums.com/ajax-upload/. You can grab the image file asynchronously, save it to disk, and return the saved image path (etc.) as a JsonResult. The javascript would look something like this:

new AjaxUpload('image-input', {
    action: '/Photo/Add',
    onComplete: function (file, response) {
        //using Fowler's json2
        var photo = JSON.parse(response);

        if (photo.IsValid) {
            //do what you want with your photo
            //photo.Path should contain the saved image's location
        }
        else {
            alert("Sorry. You tried to upload an invalid file.");
        }
    }
});

And in your Photo controller:

    [HttpPost]
    public JsonResult Add(HttpPostedFileBase UserFile)
    {
        if (UserFile == null)
        {
            //something bad happened, no file
        }

        string filePath = "/Path/To/Image/Image.jpg";

        //this isn't unit test friendly.
        //just quick and dirty:
        string fullPath = HostingEnvironment.MapPath(filePath);
        UserFile.SaveAs(fullPath);

        var model = new JsonPhotoAdd
        {
            IsValid = true,
            Path = filePath
        };

        return Json(model, "text/html");
    }

Keep in mind: this is a workaround, as browsers guard against asynchronous file uploads. As such, you might want to consider adding a second page to your form submission process. On that second page, users can handle uploading images with a standard file input field and a form post. http://forums.asp.net/p/1237419/2253919.aspx

ewwwyn
HeyThanks for the answer. I need to create a image upload (which will be created on a separate page) and this provides a great example.What I'm trying to do is to create a control that will be displayed in a popup that allows the user to select an already uploaded image. The control will have a list of images, and the user can select only one image. I'm just not sure how to return from the modal popup with just the image name that can be place on my view model without losing previous changes.I will look into doing an ajax call like above when the use selects an image and hits sumbit.
kheit
Hey kheit,Dave Thieben's suggestion is probably what you're looking for, then. If that doesn't quite add up, perhaps you could provide the code that you're working with?
ewwwyn
A: 

I would do it with a hidden input field on the main form. when the user clicks an image in the modal popup, use jQuery to handle the click event and assign the value of the image clicked (the filename?) to the hidden input field. if you name the hidden input field correctly, the ModelBinder will automatically bind the filename to your model object, and you won't need to update it manually.

on main form:

<%= Html.HiddenFor( x => x.ImageFileName ) %>

some jQuery:

$("#popup a.candidateImage").click(function() {
    $("form input[name=ImageFileName]").value = $("img", this).attr("src");
    // probably also close the modal?
}

I didn't test this, but you should get the idea... I would probably also try to show a thumbnail of the file in ImageFileName on the main form if it's been populated.

dave thieben
A: 

Hey!

i use ASP.NET MVC2 and the NEW VERSION of AjaxUploader:

when i call my Controller:

"public JsonResult AddImg(HttpPostedFileBase UserFile)"

UserFile is always null...

the script:

function createUploader() { var uploader = new qq.FileUploader({ element: document.getElementById('file-uploader-demo1'), action: '/Evenement/AddImg', name: 'UserFile', }); }

in my view, the form is OK

<% using (Html.BeginForm("AddImg", "Evenement", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>

<% } %>

do you have an Idea ? Many thanks and sorry for my bad english^^ i'm french.

Electro