tags:

views:

96

answers:

1

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.

+1  A: 

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.

richeym