user2862542
user2862542

Reputation: 205

Return Model but it's null

I'm using Imageresizer and uploading a photo. I want to get the URL that I receive and return it to my View. I have the URL that I want, but I can't return it further... This is my code at the moment:

public ActionResult UploadPhoto(Image img, HttpPostedFileBase file, ProfilePageModel model)
{
    var currentUser = CommunitySystem.CurrentContext.DefaultSecurity.CurrentUser;
    bool isAccepted = false;

    string fileName = string.Empty;
    if (file.ContentLength > 0)
    {
        string fName = System.Guid.NewGuid().ToString();
        // Generate each version
        foreach (string fileKey in System.Web.HttpContext.Current.Request.Files.Keys)
        {
            HttpPostedFile uploadFile = System.Web.HttpContext.Current.Request.Files[fileKey];
            // Generate a filename (GUIDs are best).
            fileName = Path.Combine("~/uploads/", fName + format);

            // Let the image builder add the correct extension based on the output file type
            ImageBuilder.Current.Build(uploadFile, fileName, new ResizeSettings(
                "width=300;format=jpg;mode=max;crop=20,20,80,80;cropxunits=100;cropyunits=100"));
            model.fileName = fileName;        
        }
        return RedirectToAction("EditProfile");
    }
    return RedirectToAction("EditProfile");
}

When I run this, "uploading a file and press Submit" I get that the model is null, the model contains information about a profile like the name, what country you're from, and so on.

I somehow need to add values to my model so I can return it (so it won't crash). Any ideas?

Upvotes: 2

Views: 455

Answers (2)

Ahmed ilyas
Ahmed ilyas

Reputation: 5822

first problem is that from the code, it seems you are NOT returning the model in your RedirectToAction methods, of course depends if you are wanting to do this.

but if the model is null then it means that the items are not being posted back to the controller action.

Upvotes: 2

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

For the model to have values, its properties need to exist in the form. You'll need to at least add hidden fields for each property you want sent back on a POST.

Upvotes: 2

Related Questions