MVC PartialView如何在使用PartialView上传后保持在同一页面?

时间:2016-12-05 07:45:52

标签: c# asp.net-mvc

我有三个页面,它包含一个PartialView,它是一个成员数据信息面板。

PartialView可以使用Html.BeginForm更改照片。

但我在提交照片时遇到问题,它无法返回同一页面。

如何解决?

代码:

查看(页面,有三个不同的页面,但都有相同的PartialView):

    <div>Page 1</div>
    <div class="sidebar">
        @Html.Action("_MemberInfoPartial")
    </div><!-- END sidebar-->
    <div>blah blah ... </div>

查看(部分):

    <figure class="cropper">
            <a>@Model.UserName</a>
            <img src="@Model.Image" class="photo">
            <figcaption><a href="javascript:;">Select Pic</a></figcaption>
            @using (Html.BeginForm("UploadIcon", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <input type="file" name="file" id="file" class="temp-icon-file" />
                <input type="submit" name="submit" value="Submit" class="temp-icon-submit" />
            }
            <script>
                $(function () {
                    $('.cropper figcaption a').click(selectFile);
                    $('.temp-icon-file').change(uploadFile);
                });
                function selectFile() {
                    $('.temp-icon-file').click();
                }
                function uploadFile() {
                    var val = $('.temp-icon-file').val().toLowerCase().split('.');
                    if (val.length > 0) {
                        var ext = val[val.length - 1];
                        if (ext === 'jpg' || ext === 'png') {
                            $('.temp-icon-submit').click();
                            return;
                        }
                    }
                }

            </script>
        </figure>

控制器:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UploadIcon(HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            //Upload Image
        }
        else
            TempData["TempMessage"] = "Please Select Picture! (jpg/png)";

        return RedirectToAction("Page1") <--How to return to same page(The Page I click upload, it can be page1 or 2 or 3)?
    }

1 个答案:

答案 0 :(得分:1)

如果在调用子操作时传递当前操作名称:

    public ActionResult _MemberInfoPartial(string parentAction)
    {
        //...
        ViewBag.ParentAction = parentAction;
        //...
    }

并在您的子操作中,将其存储在ViewData中:

@{
    string parentAction = ViewBag.ParentAction;
}
@using (Html.BeginForm("UploadIcon", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <input type="file" name="file" id="file" class="temp-icon-file" />
    <input type="submit" name="submit" value="Submit" class="temp-icon-submit" />
    @Html.Hidden("returnAction", parentAction)
}

在隐藏字段中呈现父操作,例如:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UploadIcon(HttpPostedFileBase file, string returnAction)
    {
        if (file != null && file.ContentLength > 0)
        {
            //Upload Image
        }
        else
            TempData["TempMessage"] = "Please Select Picture! (jpg/png)";

        return RedirectToAction(returnAction);
    }

您可以在表单提交时使用父操作:

Thread