Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.net MVC4中Html.BeginForm中的多个按钮中的问题_C#_Jquery_Html_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# ASP.net MVC4中Html.BeginForm中的多个按钮中的问题

C# ASP.net MVC4中Html.BeginForm中的多个按钮中的问题,c#,jquery,html,asp.net-mvc,asp.net-mvc-4,C#,Jquery,Html,Asp.net Mvc,Asp.net Mvc 4,我正在用asp.NETMVC4用c做我的项目# 我正在尝试选择一张照片并将其保存在文件夹中 为此,我有一个html开始表单 @using (Html.BeginForm("ImageReplace", "Member", new { imgid = @Model.Id }, FormMethod.Post,new { enctype = "multipart/form-data" })) { <input type="text" name="fake_section" c

我正在用asp.NETMVC4用c做我的项目#

我正在尝试选择一张照片并将其保存在文件夹中

为此,我有一个html开始表单

 @using (Html.BeginForm("ImageReplace", "Member", new { imgid = @Model.Id }, FormMethod.Post,new { enctype = "multipart/form-data" }))
  { 
     <input type="text" name="fake_section" class="fake_section"><button class="fake_section">Choose Photo</button>
     <input type="file" style="display:none;" name="file" id="file" value="Choose Photo" accept="image/png,image/jpeg" /> 
     <input type="submit" name="submit" value="Submit" />
   }
为了对表单进行样式设置和验证(仅选择gif和jpeg文件),我使用以下jquery

 $('.fake_section').click(function (e) {
    e.preventDefault();

    $('#file').trigger('click');
});

$('#file').change(function (e) {
    var filename = $(this).val();
    var ext = filename.split('.').pop().toLowerCase();

    if ($.inArray(ext, ['gif', 'jpg', 'jpeg']) == -1) {
        alert('not valid!');
    }
    else {
        $('input[name="fake_section"]').val(filename);
    }
});


我的问题是,当单击选择照片时,直接转到控制器操作,而不选择照片

我认为按钮类型丢失。 看看这个JSFIDLE:

选择照片

将按钮更改为
是否有帮助?现在不转到控制器。。但是不选择图像通过
不选择图像
您的意思是文件选择器对话框没有打开吗?是否包含正确版本的jquery?您也可以尝试
$('#file')。单击() $('.fake_section').click(function (e) {
    e.preventDefault();

    $('#file').trigger('click');
});

$('#file').change(function (e) {
    var filename = $(this).val();
    var ext = filename.split('.').pop().toLowerCase();

    if ($.inArray(ext, ['gif', 'jpg', 'jpeg']) == -1) {
        alert('not valid!');
    }
    else {
        $('input[name="fake_section"]').val(filename);
    }
});
<button class="fake_section" type="button">Choose Photo</button>