Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:36:29+00:00 2026-06-02T17:36:29+00:00

I have a page where you can edit a client. On this page, you

  • 0

I have a page where you can edit a client. On this page, you can also upload files for a client. You click a button, a jQuery dialog opens, you upload a file, and click Save.

Here’s my /Clients/Edit view:

<script type="text/javascript" language="javascript">

    $(document).ready(function () {

        // add file click event
        $("a#addFile").click(function () {
            $.ajax({
                url: '@Url.Content("/ClientFiles/Create/")' + @Model.ClientId,
                context: document.body,
                success: function (data) {
                    // open dialog with Create partial view data
                    $("#dialog-add").html(data).dialog("open");
                }
            });

            return false;
        });


        // add file dialog settings
        $("#dialog-add").dialog({
            modal: true,
            buttons: {
                "Save": function () {
                    $.validator.unobtrusive.parse("#AddFileForm");
                    if ($("#AddFileForm").valid()) {
                        $.post("/ClientFiles/Create", $("#AddFileForm").serialize(),
                            function (data) {
                                $("#dialog-add").dialog("close");   // close dialog
                            });
                    }
                }
            }
        });
    });

</script>

@using (Html.BeginForm("Edit", "Clients", FormMethod.Post))
{
    @Html.HiddenFor(m => m.ClientId)            

    <a id="addFile">Add a new file</a>
    <div id="dialog-add" title="" style="display:none"></div>        

    <input type="submit" value="Save" />            
}

Here is my /ClientFiles/Create partial view:

@model Models.ClientFileViewModel

@using (Html.BeginForm("Create", "ClientFiles", FormMethod.Post, new { @id = "AddFileForm", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.ClientId)    
    @Html.TextBoxFor(model => model.ModelFile, new { type = "file" })

    @Html.ValidationSummary()
}

Here’s my ClientFileViewModel:

public class ClientFileViewModel
{
    [ScaffoldColumn(false)]
    public int ClientFileId { get; set; }

    public int ClientId { get; set; }
    public HttpPostedFile ModelFile { get; set; }
}

And finally, my ClientFilesController methods:

public ActionResult Create(int id)
{
    return PartialView(new ClientFileViewModel { ClientId = id  } );
}

public JsonResult Create(ClientFileViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        if (viewModel.ModelFile != null)
        {
            // upload file here
        }
    }

    return Json(new { success = true });
}

So everything works up until I post to Create(ClientFileViewModel viewModel). When I go through the debugger, the view model contains ClientId, but ModelFile is null.

Am I doing something wrong that is causing the file to not be passed with the view model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:36:30+00:00Added an answer on June 2, 2026 at 5:36 pm

    You should use the base class (HttpPostedFileBase), i.e. replace:

    public HttpPostedFile ModelFile { get; set; }
    

    with:

    public HttpPostedFileBase ModelFile { get; set; }
    

    in your view model.


    This being said, that’s only half of your issues. You seem to be using an AJAX request to submit the form:

    $.post("/ClientFiles/Create", $("#AddFileForm").serialize(),
    

    You cannot use AJAX to upload files to the server. You could use some plugin such as jquery.form which supports file input fields and generates a hidden iframe to circumvent this limitation. So if you decide to use the plugin you could do the following to AJAXify it:

    "Save": function () {
        $.validator.unobtrusive.parse("#AddFileForm");
        if ($("#AddFileForm").valid()) {
            $("#AddFileForm").ajaxSubmit(function() {
                $("#dialog-add").dialog("close");
            });
        }
    }
    

    Other popular plugins that allow you to upload files are Uploadify, Blueimp File Upload, Ajax Upload, …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cancel button on a page. But this page can be opened
I have a page where the user can edit various content using buttons and
I have a page where users can select different documents files in a datalist
I have a table of pages in my database, each page can have a
I have page, i can contact directly without any problems. http://www.mysite.com/app_dev.php/comments/22 the page shows
I have page which is redirected from htaccess. now I can pass the German
Let's say you have a profile page that can only be accessed by the
i would like to have a page where i can change passwords. me should
I have two related tables in my database: Page and Tag. One Page can
I have a page where a user can import data to the site. either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.