I want to edit a user details and in the same time Upload a photo with the same button, but they use different controllers and the Upload View is called as an @Html.Action (using a different razor view) and is always null if I try to submit along with user info, I don't know how to deal with this.

Edit Controller:

public async Task<ActionResult> Edit([Bind(Include = "Id, Email, Name, Surname, City, ZIP, Street, Nr, CompName, CUI, DescCompany")] EditUserViewModel editUser, Image img, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByIdAsync(editUser.Id);

            if (user == null)
            {
                return HttpNotFound();
            }

            else 
            {
                 //fetch stuff here               
            }

            var otherController = DependencyResolver.Current.GetService<ImageController>();
            //var result = otherController.Upload(img, file);
            otherController.ControllerContext = new ControllerContext(Request.RequestContext, otherController);

            return RedirectToAction("Details", "Jobs", new { controller = "JobsController", action = "Details", id = Session["DetailsURL"] });

        }

After else { } statement I tried to bring somehow ImageController and call the ActionResult Upload, I don't understand how I can make the Edit User View to take the data from Upload Image View.

Edit View:

@Html.Action("Upload", "Image")

<h2>Company Details</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        //bunch of generated divs for editing 

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

I guess I am not allowed to just call a View just with @Html.Action for this purpose.

Image Controller:

I don't think is relevant, its working fine if I use the Upload button from its View and its throw null if I try to use along side with Edit User View.

An exception of type 'System.NullReferenceException' occurred in JobSite3.dll but was not handled in user code

Image View:

@using (Html.BeginForm("Upload", "Image", null, FormMethod.Post, new { enctype = "multipart/form-data" } ))
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => Model.ID);
    <table>
        <tr>
            <td>Select File : </td>
            <td>
                @Html.TextBoxFor(Image => Model.File, new { type = "file" })
                @Html.ValidationMessage("CustomError")
            </td>

                <td>

                    <input type="submit" value="Upload"/>

                </td>

        </tr>
    </table>
}

It might be my approach wrong, since I don't really have much experience with MVC. Any suggestions how I can use a single button that submit data from 2 different views to coresponding ActionResults?

Related posts

Recent Viewed