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 9320075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:32:39+00:00 2026-06-19T03:32:39+00:00

I use Jquery Ajax Form Plugin to upload file. Codes: AuthorViewModel public class AuthorViewModel

  • 0

I use Jquery Ajax Form Plugin to upload file. Codes:

AuthorViewModel

public class AuthorViewModel
{
    public int Id { get; set; }

    [Required(ErrorMessage = "{0} alanı boş bırakılmamalıdır!")]
    [Display(Name = "Yazar Adı")]
    public string Name { get; set; }

    [Display(Name = "Kısa Özgeçmiş")]
    public string Description { get; set; }

    [Display(Name = "E-Posta")]
    public string Email { get; set; }

    public string OrginalImageUrl { get; set; }

    public string SmallImageUrl { get; set; }
}

Form

@using (Html.BeginForm("_AddAuthor", "Authors", FormMethod.Post, new { id = "form_author", enctype = "multipart/form-data" }))
{
    <div class="editor-label">
        <input type="file" name="file" id="file" />
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    ...

    <div class="submit-field">
        <input type="submit" value="Ekle" class="button_gray" />
    </div>
}

Script

<script>
$(function () {
    $('#form_author').ajaxForm({
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
    });
});

function ShowRequest(formData, jqForm, options) {
    $(".loading_container img").show();
}

function AjaxError() {
    alert("An AJAX error occured.");
}

function SubmitSuccesful(result, statusText) {
    // Veritabanı işlemleri başarılı ise Index sayfasına
    // geri dön, değilse partial-view sayfasını yenile
    if (result.url) {
        window.location.href = result.url;
    } else {
        $(".authors_content_container").html(result);
    }
}
</script>

Controller

[HttpPost]
public ActionResult _AddAuthor(AuthorViewModel viewModel, HttpPostedFileBase file)
{
    ...
    viewModel.OrginalImageUrl = file.FileName;
    ...
}

Above codes work fine

Question

As you see, I post file seperate from ViewModel. Is there a way to add HttpPostedFileBase file property to ViewModel and bind it to viewModel in view, And post it to controller in ViewModel?

I hope , I can explain.

EDIT:

This codes work fine. I dont want post , viewModel and HttpPostedFile seperately. I want something like this: (If it is possible.)

Model

public class AuthorViewModel
{
    public int Id { get; set; }

    [Required(ErrorMessage = "{0} alanı boş bırakılmamalıdır!")]
    [Display(Name = "Yazar Adı")]

    HttpPostedFileBase file{ get; set; }
    ...
}

Controller

[HttpPost]
public ActionResult _AddAuthor(AuthorViewModel viewModel)
{
    var file = viewModel.file;
    ...
}    

Thanks.

  • 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-19T03:32:41+00:00Added an answer on June 19, 2026 at 3:32 am

    Yes you can add AliRıza Adıyahşi.

    Here is the property to do it:

    public HttpPostedFileBase File { get; set; }
    

    Now in you form you should add enctype as Xiaochuan Ma said:

    @using (Html.BeginForm("_AddAuthor", "Authors", FormMethod.Post, new { id = "form_author", enctype="multipart/form-data" }))
    {
        <div class="editor-label">
            <input type="file" name="file" id="file" />
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    
        ...
    
        <div class="submit-field">
            <input type="submit" value="Ekle" class="button_gray" />
        </div>
    }
    

    On you Controller action:

    [HttpPost]
    public ActionResult _AddAuthor(AuthorViewModel viewModel, HttpPostedFileBase file)
    {
        if(file!=null)
        {
            viewModel.File=file; //Binding your file to viewModel property
        }
        //Now you can check for model state is valid or not.
        if(ModelState.IsValid)
        {
            //do something
        }
        else
        {
            return View(viewModel);
        }
    }
    

    Hope it helps. Is this what you need ?


    EDIT

    There is nothing additional to do. Its automatically binding to viewModel.

    [HttpPost]
    public ActionResult _AddAuthor(AuthorViewModel viewModel)
    {
          var uploadedfile = viewModel.File;// Here you can get the uploaded file.
          //Now you can check for model state is valid or not.
        if(ModelState.IsValid)
        {
            //do something
        }
        else
        {
            return View(viewModel);
        }
    }
    

    This worked for me !

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

Sidebar

Related Questions

I'm trying to use the JQuery Form plugin (http://jquery.malsup.com/form/) to upload a file and
I have a small jQuery plugin that I use for form AJAX validation. There
I am trying to use jquery validator plugin and form submit via ajax in
I'm trying to use codeigniter and the Blueimp Jquery File Upload plugin for the
I'm trying to use the jQuery form plugin ( http://jquery.malsup.com/form ) to upload and
I use a jQuery plugin called jscroller, which uses jquery.ajax to make ajax calls.
Possible Duplicate: jquery validate & ajax.beginform I'm trying to use the jQuery validate plugin
I'm trying to use jQuery Form Plugi n to handle file uploads in an
I'm trying to use the jQuery forms plugin to dynamically submit a form on
I'm trying to use the jquery validate plugin to validate a form and submit

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.