将值绑定到Asp.Net MVC应用程序中的模型 [英] Bind value to model in Asp.Net MVC Application

查看:59
本文介绍了将值绑定到Asp.Net MVC应用程序中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型中,我有一个HttpPostedFileBase属性作为File.在视图中,我有一个文本框"A"和一个按钮"B".我的页面上还有一个隐藏的输入type ="file" id ="file".在B上单击我触发#file.click在我的JavaScript中.然后,所选文件应绑定到模型属性,并且文件名应显示在文本框中.我无法做到这一点.有什么帮助吗?我希望这个问题很清楚,如果不能的话,请告诉我,以便我进一步阐述.

In my model i have a HttpPostedFileBase property as File. In the view I have a textbox "A" and a button "B". I also have a hidden input type="file" id ="file "on my page. On B click i trigger the #file.click in my javascript. The selected file should then bind to the model property and the file name should be displayed on the textbox. I am unable to do this. Any help? I hope the question is clear, if not please tell me so that i can elaborate further.

有帮助吗?

型号:

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属性

Assign File to the name property of your file input

    <input type="file" name="File" id="fileInputType" style="visibility:hidden"/> 

这将在您提交表单时绑定到您的操作方法中的HttpPostedFileBase file参数.

And this would bind to HttpPostedFileBase file parameter in your action method when you submit the form.

记住将表单的entype设置为multipart/form-data

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new {enctype="multipart/form-data" })

这篇关于将值绑定到Asp.Net MVC应用程序中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆