Remember that since these are partial views, this does not mean that they do not belong to the current DOM.
That means if I have js
function ClickEventListener(partialView)
{
alert(partialView.innerHTML);
}
And I have this:
<div id="view_1" onclick="ClickEventListener(this)">
</div>
<div id="view_2" onclick="ClickEventListener(this)">
</div>
<div id="view_3" onclick="ClickEventListener(this)">
</div>
Will work correctly. With that in mind, you can share a partial view with each other, edit its contents (by assigning an identifier or class that depends on how you want it), and you can only have one ajax to request a server to respond and edit a partial view desires.
Here is an example:
These are the 3 views that are already displayed.
<div id="view_1" onclick="ClickEventListener(this)">
</div>
<div id="view_2" onclick="ClickEventListener(this)">
</div>
<div id="view_3" onclick="ClickEventListener(this)">
</div>
<script>
function ClickEventListener(partialView)
{
var id;
$.get('localhost/ChangeBGColor',{id: $(partialView).prop('id')},function(response){
$(response.partialTarget).css({background:response.color});
})
}
}
<script>
Server:
public JsonResult ChangeBGColor(int id){
object response;
switch(id)
{
case 1:
response = new { partialTarget= 'view_2',color = 'blue'};
break;
case 2:
response = new { partialTarget= 'view_3',color = 'black'};
break;
case 3:
response = new { partialTarget= 'view_1',color = 'green'};
break;
}
return Json(response,JsonRequestBehavior.AllowGet);
}
If you need to update its contents, you can also do this.
Tip:
, javascript, , , . , , .., .. , .