In your example, Model is defined as
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}
Which already has an identifier. So to send this identifer from client side to server side, you should define the variable at form. So you should change the partial view as
@model PartailViewAutoRefresh.Models.Model
@Html.HiddenFor(model => model.ID)
<img src="@Model.ImageUrl" alt="@Model.Name" title="@Model.Name" width="150px" height="150px"/>
Now the ajax should be invoked as
<script type="text/javascript">
$(function () {
var contributorId = $("#ID").val();
setInterval(function () { $('#contributors').load('@Url.Action("GetContributor", "Home" })?id='contributorId); }, 3000); // every 3 sec
});
</script>
Server should change the action as
public ActionResult GetContributor(int id)
Few changes you can make from your example are
1. You should change GetContributor
to HttpPost
.
[HttpPost]
public ActionResult GetContributor(int id)
- Instead of .load method which calls HttpGet action so for security perspective you should change it to HttpPost; from client side use $.ajax or $.post.
setInterval(function () { $.ajax({
url: "@Url.Action("GetContributor", "Home"),
method: "POST",
data: { id : contributorId },
dataType: "html",
success : function(data) {
$('#contributors').html(data);
}
});
}, 3000);
});