如果嵌套在视图模型中,则不绑定IFormFile

时间:2017-03-20 18:15:23

标签: razor asp.net-core-mvc .net-core model-binding

我遇到在.net核心mvc项目中绑定嵌套IFormFile的问题。 如果我将IFormFile置于嵌套视图模型中,它将不会在帖子上绑定到它。 例如,这不起作用:

public class SomeVM
{
  public GalleryVM Gallery { get; set; }
}
public class GalleryVM
{
  public IFormFile UploadingImage { get; set; }
  //gallery properties...
}

查看:

@model SomeVM

    <form method="post" enctype="multipart/form-data">
    <input type="file" name="Gallery.UploadingImage" />
    <input type="submit" value="save" />
    </form>

为简洁起见,省略了一些代码。

1 个答案:

答案 0 :(得分:2)

我找到了解决方案,所以我想与你分享。我发现它是已知问题,应该在.net core 2.0 issue on github

中解决

当前的黑客攻击是在上传文件时发送一些额外的数据。

    <div id="footer">
        <form action="aPage.html" method="post">
            <input type="button" value="Simulate to next round!" /><input type="button" onclick="saveDragDropNodes();download()" value="Download" />
        </form>
    </div>
    <div id="dhtmlgoodies_dragDropContainer">
        <div id="dhtmlgoodies_listOfItems">
            <div>
                <p>
                    Available Players
                </p>
                <ul id="Available Players">
                    <li id="node7">Player A
                    </li>
                    <li id="node8">Player B
                    </li>
                    <li id="node9">Player C
                    </li>
                    <li id="node10">Player D
                    </li>
                    <li id="node11">Player E
                    </li>
                    <li id="node12">Player F
                    </li>
                    <li id="node13">Player G
                    </li>
                    <li id="node14">Player H
                    </li>
                    <li id="node15">Player I
                    </li>
                    <li id="node16">Player J
                    </li>
                    <li id="node17">Player K
                    </li>
                    <li id="node18">Player L
                    </li>
                    <li id="node19">Player M
                    </li>
                    <li id="node20">Player N
                    </li>
                    <li id="node21">Player O
                    </li>
                    <li id="node22">Player P
                    </li>
                    <li id="node23">Player Q
                    </li>
                    <li id="node24">Player R
                    </li>
                    <li id="node25">Player S
                    </li>
                    <li id="node26">Player T
                    </li>
                    <li id="node27">Player U
                    </li>
                    <li id="node28">Player V
                    </li>
                    <li id="node29">Player W
                    </li>
                    <li id="node30">Player X
                    </li>
                    <li id="node31">Player Y
                    </li>
                    <li id="node32">Player Z
                </ul>
            </div>
        </div>
        <div id="dhtmlgoodies_mainContainer">
            <!-- ONE <UL> for each "room" -->
            <div>
                <p>
                    Team A
                </p>
                <ul id="box1">
                    <li id="node1">Captain A
                    </li>
                </ul>
            </div>
            <div>
                <p>
                    Team B
                </p>
                <ul id="box2">
                    <li id="node2">Captain B
                    </li>
                </ul>
            </div>
            <div>
                <p>
                    Team C
                </p>
                <ul id="box3">
                    <li id="node3">Captain C
                    </li>
                </ul>
            </div>
            <div>
                <p>
                    Team D
                </p>
                <ul id="box4">
                    <li id="node4">Captain D
                    </li>
                </ul>
            </div>
            <div>
                <p>
                    Team E
                </p>
                <ul id="box5">
                    <li id="node5">Captain E
                    </li>
                </ul>
            </div>
            <div>
                <p>
                    Team F
                </p>
                <ul id="box6">
                    <li id="node6">Captain F
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <div id="footer">
        <form action="aPage.html" method="post">
            <input type="button" value="Simulate to next round!" /><input type="button" onclick="saveDragDropNodes();download()" value="Download" />
        </form>
    </div>
    <ul id="dragContent"></ul>
    <div id="dragDropIndicator">
        <img src="images/insert.gif" />
    </div>
    <div id="saveContent" align="center"></div>