2014-09-22 48 views
1

我的主要(MyProfile)視圖包含鏈接,當用戶單擊鏈接時,部分視圖將加載數據庫中的現有數據並由用戶更新。MVC4將數據加載到局部視圖中

@Ajax.ActionLink("Update 1", "Update1", new { email = @ViewBag.Email }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "divCurrentView", InsertionMode = InsertionMode.Replace }) 

@Ajax.ActionLink("Update 2", "Update2", new { email = @ViewBag.Email }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "divCurrentView", InsertionMode = InsertionMode.Replace }) 

<div id="divCurrentView"> 
</div> 

局部視圖:例如:

_Update1:​​

@model ViewModels.Update1 
@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 
@Html.ValidationSummary(true)        
@Html.HiddenFor(model => model.id) 

@Html.LabelFor(model => model.Name) 
@Html.TextBoxFor(model => model.Name) 
<input type="submit" value="Update" /> 
} 

_Update2:

@model ViewModels.Update2 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true)        
    @Html.HiddenFor(model => model.id) 

    @Html.LabelFor(model => model.Website) 
    @Html.TextBoxFor(model => model.Website) 
    <input type="submit" value="Update" /> 
} 

在Controller:

public PartialViewResult Update1(string email) 
    { 
     var model = populate the viewmodel 
     return PartialView("_Update1",model); 
    } 
public PartialViewResult Update2(string email) 
    { 
     var model = populate the viewmodel 
     return PartialView("_Update2",model); 
    } 

這並不意味着用戶在訪問主視圖時將點擊所有鏈接。

如果我的方式正確,我想獲得反饋或者當用戶獲取MyProfile時是否應該加載所有數據?在會話中查看和存儲數據以及何時從會話加載每個分部視圖? 這會避免每次partilaview被加載時調用數據庫,還是有更好的方法?

感謝,

UPDATE:

我嘗試使用緩存的建議,但問題的數據是全球存儲。如果多個用戶登錄並嘗試查看/更新數據,那麼所有數據都是相同的我錯過了什麼?

這是嘗試:

public PartialViewResult Update1(string email) 
    { 
     var cc = HttpRuntime.Cache.Get("cinfo"); 
     Update1VM model = null; 
     if (cc == null) 
     { 
      model = populate the viewmodel 
      HttpRuntime.Cache.Insert("cinfo", model); 
     } 
     else 
     { 
      model = (Update1VM)cc; 
     } 

     return PartialView("_Update1", model); 
    } 
+0

取決於我們在會話中存儲多少數據。如果它不是很多,那肯定是有效的。如果沒有,那麼你可能不想將它存儲在會話中。 – Gjohn 2014-09-22 15:29:48

+0

多少數據是多少?我有大約20個領域。 – Ben 2014-09-22 15:41:34

+0

它會始終保持20場?這會改變嗎?請記住,有一天你不會在那裏,其他人將需要處理你創建的內容。就我個人而言,從來沒有一個在會話中存儲的東西的巨大風扇,只是因爲往返數據庫的往返行程是不希望的。 py3rstr的repsonse在需要時提供數據,而不是始終加載所有內容。 – Gjohn 2014-09-22 15:47:32

回答

1

使用Html.ActionLink在你使用的方法是很好的解決方案。它使控制器簡單和可重複使用。您可以輕鬆添加和刪除視圖,而無需更改控制器。將它們置於不同的視野很容易。 通常它是更靈活的解決方案。

如果你害怕每次總是使用一些緩存時調用數據庫,但在你的例子中,只有當用戶真正需要時纔會查詢數據庫並點擊它。

如果你把它放在一個視圖中,它會更復雜,更雜亂,更不容易出錯。

+0

謝謝。場景用戶點擊update1鏈接,然後點擊update2鏈接(前後)×10次而不修改數據。爲了避免調用db額外的時間,我假設我應該使用緩存?或者如果用戶更新一個partilaview的數據,我該如何將它存儲在緩存中?你能提供一個我如何做的例子嗎? – Ben 2014-09-22 16:09:01

+0

看看[System.Web.Caching.Cache](http://msdn.microsoft.com/en-us/library/system.web.caching.cache(v = vs.100))。aspX)你可以從數據庫中緩存數據並在更新時刪除請參閱[示例](http://patrickdesjardins.com/blog/using-the-system-web-cache-object-into-your-mvc3-project)。您還可以使用'Html.Action'幫助器以單個請求呈現所有子項操作。還有一些jquery tab插件可以在表單之間切換。在這種情況下,用戶可以根據需要多次點擊不創建數據庫查詢並且動作仍然是分開的。 – py3r3str 2014-09-22 16:59:16

+0

當你說在一個請求中渲染所有的孩子動作。你的意思是在我的主視圖(myprofile)中將所有字段捕獲到1個viewmodel中,然後爲每個partialview添加選項卡並從一個viewmodel加載數據?所有的partialviews都使用相同的viewmodel? – Ben 2014-09-22 18:02:29