2016-12-05 41 views
1

我有三个页面,它全部包含一个PartialView,它是一个成员数据信息面板。MVC PartialView如何在使用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)? 
    } 
+0

AFAIK,你可以使用'变种名称=获取与Razor视图名称(如this.ViewContext.View RazorView).ViewPath'或'变种名称= ViewContext.RouteData .Values [“action”]。ToString()',然后用'return View(name)'返回视图。 –

+0

@TetsuyaYamamoto但它返回'UploadIcon',而不是页面操作名称... – LorenzoC

回答

1

如果调用子操作当你通过电流动作名称:

@Html.Action("_MemberInfoPartial", new { parentAction = ViewContext.RouteData.Values["action"] }) 

,并在你的孩子的行动,将其存储在ViewData的:

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

渲染家长行动中隐藏字段,例如:

@{ 
    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); 
    } 
0

将值传递给您的控制器以确定您需要返回的页面。

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

    if(returnId == 1) 
     return RedirectToAction("Page1"); 
    //and so on.. 
}