2012-01-12 71 views
0

我正在使用彈出窗口中顯示的局部視圖來編輯主窗口中單擊的記錄的詳細信息。在這個視圖中,用戶可以編輯,刪除或取消。當細節記錄被成功編輯或刪除時,我想關閉局部視圖並更新主窗口。現在,在刪除時,我的主要觀點是在成功時彈出重新加載。如何關閉在彈出窗口中打開的局部視圖並刷新主頁面?

任何幫助獲得這項工作非常感謝。

的HTML:

`<a href="javascript:popup('@Url.Action("EditCaseEmployer/" + item.Id, "CaseOptions")')">@item.StartDate.ToShortDateString()</a>` 

下面是自定義JavaScript函數彈出一個窗口:

function popup(url) { 
window.open(url, 'notes', 'width=900,height=600,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); 
} 
function popup(url, windowName) { 
window.open(url, windowName, 'width=900,height=600,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); 
} 
+0

是否使用'window.open'以打開彈出? – 2012-01-12 15:04:50

+0

這是我完成這個任務時的代碼。如有必要可以更改它。 @item.StartDate.ToShortDateString() TheGeekYouNeed 2012-01-12 15:08:45

+0

什麼是'popup'?我不知道這樣的功能存在。它是否定製?如果是這樣,它看起來如何? – 2012-01-12 15:09:58

回答

0

你可以一個布爾屬性添加到您正在使用您的局部視圖模型:

public class MyViewModel 
{ 
    public MyViewModel() 
    { 
     ShouldReload = false; 
    } 
    public bool ShouldReload { get; set; } 

    // Some properties 
    public string Foo { get; set; } 
} 

然後在部分測試它的值並刷新開啓者並關閉t他彈出:

@model MyViewModel 

@if (Model.ShouldReload) 
{ 
    <script type="text/javascript"> 
     // refresh the parent window 
     window.opener.location.reload(); 

     // close the popup 
     window.close(); 
    </script> 
} 
else 
{ 
    using (Html.BeginForm()) 
    { 
     @Html.LabelFor(x => x.Foo) 
     @Html.EditorFor(x => x.Foo) 
     <button type="submit">OK</button> 
    } 
} 

現在,所有剩下的是將被分別用於顯示部分並進行處理的2控制器操作:

public ActionResult Dialog() 
{ 
    var model = new MyViewModel(); 
    return PartialView(model); 
} 

[HttpPost] 
public ActionResult Dialog(MyViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     // the model is invalid => redisplay the partial 
     return PartialView(model); 
    } 
    // TODO: at this stage the model is valid 
    // => pass it to the service layer for processing 

    // everything went fine => set the ShouldReload flag to true 
    // so that the view refreshes its opener and closes itself 
    model.ShouldReload = true; 
    return PartialView(model); 
} 
相關問題