HTML表单返回null

时间:2017-04-13 11:07:21

标签: html asp.net forms

我有这样的表格:

 <div class="col-md-7">
                        <div class="panel panel-default">
                            <div class="panel-heading"><strong>Dosya yükle</strong> <small> </small></div>
                            <div class="panel-body">
                                <form action="/" enctype="multipart/form-data" method="post">
                                    <div class="input-group image-preview">
                                        <input id="file" type="file" class="form-control image-preview-filename" >
                                        <!-- don't give a name === doesn't send on POST/GET -->
                                        <span class="input-group-btn">
                                            <!-- image-preview-clear button -->
                                            <button type="button" class="btn btn-default image-preview-clear" style="display:none;"> <span class="glyphicon glyphicon-remove"></span> Temizle </button>
                                            <!-- image-preview-input -->
                                            <div class="btn btn-default image-preview-input">
                                                <span class="glyphicon glyphicon-folder-open"></span> <span class="image-preview-input-title">Tara</span>
                                                <!--<input type="file" name="input-file-preview" />-->
                                                <!-- rename it -->
                                            </div>
                                            <button type="submit" class="btn btn-labeled btn-default"> <span class="btn-label"><i class="glyphicon glyphicon-upload"></i> </span>Yükle</button>
                                        </span>
                                    </div>
                                </form>

当我提交表单时,null值将返回给控制器。为什么它为空?我该如何解决?感谢。

3 个答案:

答案 0 :(得分:1)

使用id

,而不是使用name属性
<input id="file" type="file" class="form-control image-preview-filename" >

应该是:

<input name="file" type="file" class="form-control image-preview-filename" >

这是因为name用于指定表单中的键,而id指定对象的id

答案 1 :(得分:0)

您的评论对我来说非常清楚,如果您没有为输入指定名称,它将不会在请求中传递,这意味着您的控制器将收到null。只需为每个需要的输入指定一个名称,就可以了。

另外,请查看FormHelper以正确处理您的表单。

答案 2 :(得分:0)

View Page  

@using (Html.BeginForm("ActionmethodName", "ControllerName", FormMethod.Post, new { id = "formid" }))
 { 
   <input type="file" name="file" />
   <input type="submit" value="Upload" class="save" id="btnid" />
 }

script file



     $(document).on("click", "#btnid", function (event) {
            event.preventDefault();
            var fileOptions = {
                success: res,
                dataType: "json"
            }
            $("#formid").ajaxSubmit(fileOptions);
        });

In Controller

     [HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase file)
    {
    }