开发者

Asp.net MVC, after using jeditable (Edit in place)

开发者 https://www.devze.com 2022-12-22 21:04 出处:网络
Ok, i can use jeditable to edit-in-place some content on a page and the content will be saved to a database. But whats the best way to re get that text-content from db to show into a place holder?

Ok, i can use jeditable to edit-in-place some content on a page and the content will be saved to a database. But whats the best way to re get that text-content from db to show into a place holder?

 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

The problem is if i will use some开发者_运维知识库 hard coded text like

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

I will able to edit-in-place using jeditable but when i will refresh page it will show the same "Some text here" as its not getting data from db.


Your pseudocode implies that you want the view to be responsible for fetching the required data, which is an anti-pattern in MVC. You need to retrieve the text in your controllers action and pass it to the view, either using ViewData or a custom view model, e.g.:

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

And the view looks something like:

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

A better approach would be to create a strong-typed view model (Stephen Walther has a blog post about view models here), but the above example should illustrate how data can be passed from the controller to the view.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号