Html.TextBoxFor设置值

Stw*_*twr 3 asp.net-mvc

我在我的mvc项目中使用<input type="file" id="fileId" name="fileId"/><% = Html.TextBoxFor (x => x.FileName, new {@ class = "className", maxlength = 255, id = "fileName"})%>.我想在文本框中保存在INPUT元素中选择的文件名.我怎样才能做到这一点?

Dar*_*rov 6

你需要使用javascript来实现这一目标.这是jquery的一个例子:

$(function() {
    $('#fileId').change(function() {
        // When the user selects a file, read the selected filename
        // and set it to the textbox
        var filename = $(this).val();
        $('#fileName').val(filename);
    });
});
Run Code Online (Sandbox Code Playgroud)