Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Ajax file upload with jquery form plugin?
    primarykey
    data
    text
    <p>I use Jquery Ajax Form Plugin to upload file. Codes:</p> <p>AuthorViewModel</p> <pre><code>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; } } </code></pre> <p>Form</p> <pre><code>@using (Html.BeginForm("_AddAuthor", "Authors", FormMethod.Post, new { id = "form_author", enctype = "multipart/form-data" })) { &lt;div class="editor-label"&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; ... &lt;div class="submit-field"&gt; &lt;input type="submit" value="Ekle" class="button_gray" /&gt; &lt;/div&gt; } </code></pre> <p>Script</p> <pre><code>&lt;script&gt; $(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); } } &lt;/script&gt; </code></pre> <p>Controller</p> <pre><code>[HttpPost] public ActionResult _AddAuthor(AuthorViewModel viewModel, HttpPostedFileBase file) { ... viewModel.OrginalImageUrl = file.FileName; ... } </code></pre> <p>Above codes work fine</p> <p>Question</p> <p>As you see, I post file seperate from ViewModel. Is there a way to add <code>HttpPostedFileBase file</code> property to ViewModel and bind it to viewModel in view, And post it to controller in ViewModel? </p> <p>I hope , I can explain.</p> <p>EDIT:</p> <p>This codes work fine. I dont want post , viewModel and HttpPostedFile seperately. I want something like this: (If it is possible.)</p> <p>Model </p> <pre><code>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; } ... } </code></pre> <p>Controller</p> <pre><code>[HttpPost] public ActionResult _AddAuthor(AuthorViewModel viewModel) { var file = viewModel.file; ... } </code></pre> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload