ASP.NET MVC posted file model binding when parameter is Model

前端 未结 4 1311
粉色の甜心
粉色の甜心 2020-12-02 18:46

Is there any way to get posted files () to take part in model binding in ASP.NET MVC without manually looking at the reques

4条回答
  •  心在旅途
    2020-12-02 19:28

    It turns out the reason is that ValueProviderDictionary only looks in Request.Form, RouteData and Request.QueryString to populate the value provider dictionary in the model binding context. So there's no way for a custom model binder to allow posted files to participate in model binding without inspecting the files collection in the request context directly. This is the closest way I've found to accomplish the same thing:

    public ActionResult Create(MyModel myModel, HttpPostedFileBase myModelFile) { }
    

    As long as myModelFile is actually the name of the file input form field, there's no need for any custom stuff.

提交回复
热议问题