Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

c# - Bind value to model in Asp.Net MVC Application

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.

Any help?

Edit 1:

Model:

public class FileUploadModel
    {
        public HttpPostedFileBase File { get; set; }
        public string FileName {get;set;}
    }

View:

<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 )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assign File to the name property of your file input

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

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

Edit:

Remeber to set your form's entype to multipart/form-data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...