【问题标题】:How to bind model variable to input file type control如何将模型变量绑定到输入文件类型控制
【发布时间】:2013-08-24 05:28:50
【问题描述】:

在asp.net mvc4中工作:

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

提前致谢…………

【问题讨论】:

    标签: asp.net-mvc-4


    【解决方案1】:

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

    出于安全原因,您不能这样做。否则,您可以将控件设置为“c:\passwords.txt”或其他内容,并使用一些 javascript 来上传文件,而用户不知道您已经这样做了。

    【讨论】:

      【解决方案2】:

      是的,您可以使用一个小的 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();
          }
      

      【讨论】:

        猜你喜欢
        • 2017-03-21
        • 2011-02-07
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多