1

I am developing MVC application and using razor syntax.

In this application I am giving comment facility.

I have added a partial view, which loads the comment/Records from DB.

currently, data get loaded as soon as that view get called, which I want to avoid it.

I wan to load the data only when user click on the Button, which is on that view.

This is a code of the button.

<input type="button" value="Show" id="ShowId"  onclick="LoadData()"/>                         

And below Code should be executed when user click on the button.

 @foreach (var item in Model)
    {
        <div id="OwnerName">

         <span class="EmpName"> @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })</span>

           @Html.DisplayFor(ModelItem => item.CommentDateTime)

        </div>



        <p class="CommentP">
           @Html.DisplayFor(ModelItem => item.CommentText)
        </p>

        <br />


    }

How to do this ?

user1668543
  • 199
  • 3
  • 7
  • 21

2 Answers2

2

Here are concrete examples of loading data on demand:

Follow their logic and adapt to your case.

Update

Ajax will do the work either (look into comments). Thanks webdeveloper

Hope this helps

Display Name
  • 4,672
  • 1
  • 33
  • 43
  • I dont think I want this...do I need to write Javascript for this ? – user1668543 Sep 15 '12 at 11:31
  • Yes we do, cannot do without. Look for any load on demand in MVC topic, all them are using jquery. But its not hard at all, just look what and how they do and do the same. – Display Name Sep 15 '12 at 11:32
  • 1
    He could use alternative way with ajax helpers: [MVC 3 - Ajax load partial view into div](http://stackoverflow.com/questions/4557021/mvc-3-ajax-load-partial-view-into-div) – webdeveloper Sep 15 '12 at 11:40
  • True. What I meant to say was that you cannot do purely within ASP.NET MVC. Have to employ something else (jquery, ajax, whatever will do the work). Because I had this dilemma myself when learning these things - wanted to do everything within technology boundaries, but with time realized that adjacent technologies compliment this framework and make your life much easier, rather than harder. UPdated answer with Ajax. Thanks webdeveloper. – Display Name Sep 15 '12 at 11:55
1

You want to load comments from server after user click the button? The simplest way is use Jquery. Create action which will return PartialViewResult and then make ajax request with jquery.

webdeveloper
  • 17,174
  • 3
  • 48
  • 47