2016-12-05 55 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.. 
}