Asp.net mvc 4 如何将模型变量绑定到输入文件类型控件

Asp.net mvc 4 如何将模型变量绑定到输入文件类型控件,asp.net-mvc-4,Asp.net Mvc 4,在asp.net mvc4中工作: 我正在实现编辑功能。为此,我需要将旧值映射到控制器中的模型以及传递给视图的模型。在视图中,我无法将模型变量绑定到输入文件类型控件。 所以我想知道如何解决这个问题 提前感谢……。如果我理解正确,您希望将输入文件控件的值设置为模型中的值 由于安全原因,您无法执行此操作。否则,您可以将控件设置为“c:\passwords.txt”或其他内容,并在用户不知道您已上载文件的情况下使用一点javascript来上载文件。是的,您可以使用一个与jquery和HTML 5兼容

在asp.net mvc4中工作:

我正在实现编辑功能。为此,我需要将旧值映射到控制器中的模型以及传递给视图的模型。在视图中,我无法将模型变量绑定到输入文件类型控件。 所以我想知道如何解决这个问题


提前感谢……。

如果我理解正确,您希望将输入文件控件的值设置为模型中的值


由于安全原因,您无法执行此操作。否则,您可以将控件设置为“c:\passwords.txt”或其他内容,并在用户不知道您已上载文件的情况下使用一点javascript来上载文件。

是的,您可以使用一个与jquery和HTML 5兼容的浏览器来完成此操作

 $('#YourFormId').submit(function () {
    var formData = new FormData($('form')[0]);
    $.ajax({
        url: this.action,
        cache: false,
        type: this.method,
        data: formData,
        success: function (result) {

        },
        beforeSend: function (e) {

        },
        contentType: false,
        processData: false
    });
    // it is important to return false in order to 
    // cancel the default submission of the form
    // and perform the AJAX call
    return false;
});
我从其他地方得到了这个代码,可能是这个网站。我一直看到有人说你不能这么做,但它在IE 10+和Chrome中都能工作

ViewModel如下所示:

    public class MyModel
{
    public string mytest { get; set; }
    public string mytest2 { get; set; }
    public string status { get; set; }
    public HttpPostedFileBase  fileupload {get;set;}
}
    [HttpPost]
    public ActionResult YourActionName(YourModel model)
    {
        return whatever();
    }
行动如下:

    public class MyModel
{
    public string mytest { get; set; }
    public string mytest2 { get; set; }
    public string status { get; set; }
    public HttpPostedFileBase  fileupload {get;set;}
}
    [HttpPost]
    public ActionResult YourActionName(YourModel model)
    {
        return whatever();
    }