【发布时间】:2013-11-19 00:07:44
【问题描述】:
在我的模型中,我有一个 HttpPostedFileBase 属性作为文件。在视图中,我有一个文本框“A”和一个按钮“B”。我的页面上还有一个隐藏的输入 type="file" id ="file"。在 B 单击时,我会在我的 javascript 中触发 #file.click。然后,所选文件应绑定到模型属性,并且文件名应显示在文本框中。我无法做到这一点。有什么帮助吗?我希望问题很清楚,如果没有,请告诉我,以便我进一步详细说明。
有什么帮助吗?
编辑 1:
型号:
public class FileUploadModel
{
public HttpPostedFileBase File { get; set; }
public string FileName {get;set;}
}
查看:
<script type="text/javascript">
$(document).ready(function () {
$("#Browse").click(function () {
$("#fileIputType").trigger('click');
//now the file select dialog box opens up
// The user selects a file
// The file should get associated with the model property in this view
// the textbox should be assigned the filename
});
});
</script>
@Html.TextBox("fileTextBox", Model.FileName, new { id = "fileTextBox" })
<input type="button" id="Browse" name="Browse" value="Browse" />
<input type="file" id="fileInputType" style="visibility:hidden"/>
@Html.Hidden("ModelType", Model.GetType())
//How can i bind the selected file to the model property ( public HttpPostedFileBase File )
【问题讨论】:
-
如果您显示一些代码会有所帮助。
-
@WannaCSharp 我添加了一些代码...如果有帮助:)
-
在你的脚本中,你有 $("#fileIputType") 但在你的 html 中你有 id="fileInputType"
-
@charlieparker 这样吗?它是一个 id...这就是我们访问它的方式,不是吗?
-
我认为它显示在文本框中,但你看不到它,因为它隐藏了??
标签: c# javascript asp.net-mvc model-view-controller view