在MVC 5中使用Ajax.BeginForm帮助程序上传图片问题

时间:2016-06-18 15:10:36

标签: asp.net-mvc

我在视图中使用Ajax.BeginForm,用于上传userImage和userInformation:

@using (Ajax.BeginForm("EditProfile", "User", new AjaxOptions(){HttpMethod="POST"}, new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()

            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.ID)
            <input type="hidden" name="Name" value="@Model.Name" />
            <div class="form-group focus">

                @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } } )
                    @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">

                @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">

                @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-6">
                    @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">

                @Html.LabelFor(model => model.UserImage, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-6">
                    <input type="file" name="userImage" value="" class="form-control" style="margin-right: 14px;" />
                    @Html.ValidationMessageFor(model => model.UserImage, "", new { @class = "text-danger" })
                </div>
            </div>


            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <center>
                        <input type="submit" value="GO" class="btn btn-default" />
                    </center>
                </div>
            </div>
        }

在控制器中我有:

  public ActionResult EditProfile(Tbl_Users user, HttpPostedFileBase userImage)
    {
       //Do something
    }

当我跟踪EditProfile中的代码时,我发现userImage为空,但另一个参与者user不为空!有什么问题?

注意:我对具有相同控制器的Html.BeginForm执行相同操作,并且它可以正常工作,但是通过ajax我有问题。

这是我的Tbl_User

  public int ID { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public string Name { get; set; }
    public string Access { get; set; }
    public string Description { get; set; }
    public int UniqueKey { get; set; }
    public bool IsConfirm { get; set; }
    public bool IsActive { get; set; }
    public string UserImage { get; set; }

0 个答案:

没有答案