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 ?

1
  • Please reply somebody... Commented Sep 15, 2012 at 10:31

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I dont think I want this...do I need to write Javascript for this ?
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.
He could use alternative way with ajax helpers: MVC 3 - Ajax load partial view into div
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.