我的主要(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);
}
取決於我們在會話中存儲多少數據。如果它不是很多,那肯定是有效的。如果沒有,那麼你可能不想將它存儲在會話中。 – Gjohn 2014-09-22 15:29:48
多少數據是多少?我有大約20個領域。 – Ben 2014-09-22 15:41:34
它會始終保持20場?這會改變嗎?請記住,有一天你不會在那裏,其他人將需要處理你創建的內容。就我個人而言,從來沒有一個在會話中存儲的東西的巨大風扇,只是因爲往返數據庫的往返行程是不希望的。 py3rstr的repsonse在需要時提供數據,而不是始終加載所有內容。 – Gjohn 2014-09-22 15:47:32