2010-03-16 66 views
0

好吧,我可以使用jeditable在頁面上編輯一些內容,內容將被保存到數據庫。但是,從db獲取文本內容以顯示給佔位符的最佳方式是什麼?Asp.net MVC,使用jeditable後(編輯到位)

 
p id="paraNo34" class="editable" 
    -->What i will write here so that it will get content from a 
     db's table: [Content], where id=="paraNo34". 
/p 

的問題是,如果我會用一些硬編碼的文字像

 
    p id="paraNo34" class="editable" 
    -->Some text here 
    /p 

我將能夠使用jeditable編輯就地但是當我將刷新頁面就會顯示出相同的「一些文字在這裏「,因爲它沒有從數據庫獲取數據。

回答

1

您的僞代碼意味着您希望視圖負責獲取所需的數據,這是MVC中的反模式。您需要檢索的文本在控制器的行動,並把它傳遞給視圖,或者使用ViewData或自定義視圖模型,如:

public ActionResult Index(string id) 
{ 
    // call some method to fetch data from db 
    ViewData["ID"] = id; 
    ViewData["Content"] = content; 
    return View(); 
} 

和視圖看起來類似:

<p id='<%= ViewData["ID"] %>' class="editable"> 
    <%= Html.Encode(ViewData["Content"]) %> 
</p> 

一更好的方法是創建一個強類型的視圖模型(Stephen Walther有關於視圖模型here的博客文章),但上面的示例應該說明如何將數據從控制器傳遞到視圖。