将值绑定到 Asp.Net MVC 应用程序中的模型
Bind value to model in Asp.Net MVC Application
在我的模型中,我有一个HttpPostedFileBase属性作为File。在视图中,我有一个文本框"A"和一个按钮"B"。我还有一个隐藏的输入类型="文件"id ="文件"在我的页面上。在 B 单击时,我会触发 #file.click,在我的 JavaScript 中。然后,所选文件应绑定到模型属性,并且文件名应显示在文本框中。我无法做到这一点。有什么帮助吗?我希望问题清楚,如果没有,请告诉我,以便我进一步详细说明。
有什么帮助吗?
编辑 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 )
将File
分配给文件输入的 name 属性
<input type="file" name="File" id="fileInputType" style="visibility:hidden"/>
当您提交表单时,这将绑定到操作方法中的HttpPostedFileBase file
参数。
编辑:
将窗体的entype
设置为multipart/form-data
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new {enctype="multipart/form-data" })
您不能以编程方式在 javascript 中设置 input type='file'
元素value
属性。
为什么不将文件名显示为标签文本?在模型(视图模型)类中具有FileName
属性
@model MyViewModel
{
<label>@Model.FileName</label>
}
编辑:
基于您的代码
public class FileUploadModel
{
public HttpPostedFileBase File { get; set; }
public string FileName {get;set;}
}
在视野中
@Html.TextBoxFor(m => m.File, new { type = "file" })
在控制器中
using (MemoryStream memoryStream = new MemoryStream())
{
model.File.InputStream.CopyTo(memoryStream);
}
相关文章:
- 在MVVM视图模型中处理应用程序范围的元素
- Sails.js:我可以将一些服务/模型打包为npm并在Sails.jss应用程序中加载npm吗
- 如何在ExtJs4应用程序上为视图、存储和模型设置自定义文件夹名称
- 我的html5应用程序无法显示模型
- Node.js应用程序中从模型层异步返回数据
- 使用ajax的javascript应用程序的身份验证模型
- 父-子应用程序模型;在容器中加载整个应用程序
- 没有模型的拆分应用程序的详细信息页
- 如果外部应用程序更改了持久模型(服务器数据库),AngularJS 是否可以自动更新视图
- 将值绑定到 Asp.Net MVC 应用程序中的模型
- 在 ExtJS 4 应用程序之间共享模型
- 一个应用程序中多个模型对象的角度 CRUD
- 我有一个应用程序,我想在画布上生成 3D 模型,但我不知道要使用哪个工具
- 在 Redux 应用程序中放置特定于模型的业务逻辑的位置
- 对 Ember 应用程序进行反射/自省,以查找特定模型的实例
- 在应用程序级别替换主干同步 - 而不是通过模型或 Xollection
- 如何将模型和控制器合并到我的节点.js应用程序中
- 如何用angularjse2e测试angularjs应用程序,该应用程序具有相同ng模型的多个输入
- web应用程序模型视图中的Java脚本
- Is PhoneGap应用程序是类似Angular Js的单页应用程序模型