Click here to Skip to main content
15,915,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a html page in partial view using razor .NET. Currently I am doing whole partial view page refreshing every 5 minutes and it works fine. I plan to enhance it by reload only the div inside the html page without refreshing the whole html/partial view.
Is there any article or link that I can refer to? I know JavaScript has a .load() but it does not seems to be what I want(or have I missed out something on it?)

What I have tried:

Looked online for solution but none really hit my requirement.
Posted
Updated 10-Jul-16 23:18pm
Comments
Jamie888 11-Jul-16 5:07am    
Sir, I have looked into the link and the closest is the .load(). I have checked online again for the .load() and one thing I am not sure is can the .load() accept a function inside the JavaScript and take it as parameter? For example:
I have a function MyFunction(). Can I write like:
$( "#myDiv" ).load( "MyFunction() #myDiv" ); ?

As MyFunction() will returns many data from database, is it possible to do it?

1 solution

main view

Razor
<script type="text/javascript">
    $(document).ready(function () {
        window.setInterval(updatePartial, 1000);
    });

    function updatePartial() {
        // the second param is an empty object {}, this is where you would pass
        // your param data, but if you leave the param off a GET is used and due
        // to chaching you won't see the page change, so I passed an empty object
        // to force a POST
        $("#p").load('@Url.Action("ShowPartial", "Home")', {});
    }
</script>
    <div id="m">Main view: @DateTime.Now</div>
    <div id="p">@Html.Partial("_myPartialView")</div>

</body>
</html>


partial view

Razor
Partial view: @DateTime.Now


controller

C#
public ActionResult ShowPartial()
{
    return PartialView("_myPartialView");
}


Here is an example for passing data to the partial view

Razor
<script type="text/javascript">
    $(document).ready(function () {
        window.setInterval(updatePartial, 1000);
    });

    function updatePartial() {
        $("#p").load('@Url.Action("ShowPartial", "Home")', {"ID":$("#dataID").val(), "Name":$("#dataName").val()});
    }
</script>
<div>
    ID <input type="number" id="dataID" value="1" /> Name <input type="text" id="dataName" value="John" />
</div>
<div id="m">Main view: @DateTime.Now</div>
<div id="p">@Html.Partial("_myPartialView", new TestModel{ID=1, Name="John"})</div>


Partial view

Razor
@model YourNamespace.Models.TestModel
Partial view: @DateTime.Now, model data: @Model.ID @Model.Name


Controller

C#
public ActionResult ShowPartial(TestModel model)
{
    return PartialView("_myPartialView", model);
}


Model

C#
public class TestModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 11-Jul-16 6:09am    
5!
Jamie888 11-Jul-16 8:40am    
Sir, I have few things I dont understand here and have searched online for some explantion. Please correct me if I am wrong, could I assume that the ShowPartial is the function name in my controller while the Home is the name of my controller file?
Besides, does the empty object {} will automatically recognize my previously used parameters and use it every time is refresh? As all of the codes above would be used for refreshing purposes. Thank you.
F-ES Sitecore 11-Jul-16 8:57am    
Yes "Home" is the name of the controller. If you want to pass data to the view you'll need to pass the data via that second parameter of load. How this is done depends on what the data is, where it comes from etc, but I'll update the solution to include an example of passing data also.
Jamie888 11-Jul-16 21:59pm    
Sir, if I have multiple boxes with their own unique ID, how should I place the data correctly for each of them? Should I use a for loop?
F-ES Sitecore 12-Jul-16 4:21am    
It depends on the exact markup. You might just reference each ID explicitly, or if you could look then you could, or you could attach a "data" attribute to all the inputs you want to process and use a jquery selector to target all inputs with that data attribute. It really all depends on your specific situation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900