ASP.NET MVC使用jQuery表单插件Ajax文件上传?

ASP.NET MVC使用jQuery表单插件Ajax文件上传?

问题描述:

我使用Jquery Ajax Form Plugin来上传文件。代码:ASP.NET MVC使用jQuery表单插件Ajax文件上传?

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; } 
} 

形式

@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> 
$(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> 

控制器

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

上面的代码做工精细

问题

正如你看到的,我从后视图模型文件独立。有没有办法将HttpPostedFileBase file属性添加到ViewModel并将其绑定到视图中的viewModel,并将其发布到ViewModel中的控制器?

我希望我能解释一下。

编辑:

此代码工作正常。我不希望post,viewModel和HttpPostedFile分开。我想是这样的:(如果可能)。

型号

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; } 
    ... 
} 

控制器

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

感谢。

+0

不相似,我的代码正在工作。我问了不同的事情。 – 2013-02-13 06:47:16

是的,你可以添加AliRızaAdıyahşi。

这里是财产做到这一点:

public HttpPostedFileBase File { get; set; } 

现在你形成你应该添加enctype作为*马云说:

@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> 
} 

关于你的控制器动作:

[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); 
    } 
} 

希望它有帮助。这是你需要的吗?


编辑

没有什么额外的事情。它自动绑定到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); 
    } 
} 

这对我有效!

+0

误解,审查编辑问题。我使用插件,所以不需要enctype。代码工作... – 2013-02-13 06:47:45

+0

我想把HttpPostedFileBase放入ViewModel中。 – 2013-02-13 08:26:34

+0

@AliRızaAdıyahşi你可以把Vtt中的HttpPostedFileBase,但你怎么能填充它在你的看法。我们没有helper for' 2013-02-13 08:40:13

是的,您可以将HttpPostedFileBase添加到您的ViewModel中,并将enctype = "multipart/form-data"添加到您的From in HTML中。

检查此链接: MVC. HttpPostedFileBase is always null

+0

误解,审查编辑的问题。我使用插件,所以不需要enctype。代码工作... – 2013-02-13 06:44:58