다음 코드 줄을 사용하여 내 이벤트 뷰 index.cshtml에로드되는 뷰 구성 요소 EventsViewComponent가 있습니다.
<div id="events">
@await Component.InvokeAsync("Events", new { showPrevious = Model.ShowPrevious, showUpcoming = Model.ShowUpcoming })
</div>
다음과 같이 두 개의 확인란이 추가되었습니다.
@Html.CheckBoxFor(m => m.ShowPrevious, new { id = "cbShowPrevious", onchange = "ReloadEvents()" })
@Html.CheckBoxFor(m => m.ShowUpcoming, new { id = "cbShowUpcoming", onchange = "ReloadEvents()" })
ReloadEvents ()는 다음과 같은 Ajax 호출로 EventsViewComponent를 새로 고치려는 Javascript 함수를 나타냅니다.
function ReloadEvents() {
$.ajax({
url: '@Url.Action("ReloadEvents", "Events")',
data: {
showPrevious: document.getElementById("cbShowPrevious").checked,
showUpcoming: document.getElementById("cbShowUpcoming").checked
},
success: DoThis()
})
}
function DoThis() {
const eventsDiv = document.getElementById('events');
eventsDic.innerHTML = //HTML from EventsViewComponent
}
하지만 EventsViewComponent에서 HTML을 가져올 수없는 것 같습니다.
다음과 같이 EventsViewComponent에 대한 Default.cshtml을 작성했습니다.
@{
List<Event> events = ViewData["Events"] as List<Event>;
if (events.Count > 0)
{
<table>
//event data from the model
</table>
}
}
EventsController의 ReloadEvents 메서드와 마찬가지로 EventsViewComponent의 InvokeAsync 메서드가 공격을 받고 있지만 EventsViewComponent를 업데이트 할 수없는 것 같아서 분명히 뭔가 오해하고 있습니다.
이것이 가능한지 그리고 그것을 달성하는 방법을 조언 해 줄 수 있습니까?
EventsViewComponent에서 HTML을 가져 오려면 아래와 같이 변경해야합니다.
success: function (data) {
$("#events").html(data);
}
다음은 다음과 같은 전체 작동 데모입니다.
1. 모델 :
public class Event
{
public bool ShowPrevious { get; set; }
public bool ShowUpcoming { get; set; }
public string Data { get; set; }
}
2. ViewComponent :
public class EventsViewComponent : ViewComponent
{
List<Event> data = new List<Event>() {
new Event(){ ShowPrevious=true,ShowUpcoming=false,Data="aaa"},
new Event(){ ShowPrevious=false,ShowUpcoming=true,Data="bbb"},
new Event(){ ShowPrevious=false,ShowUpcoming=true,Data="ccc"},
};
public IViewComponentResult Invoke(bool showPrevious,bool showUpcoming)
{
if (showPrevious == true && showUpcoming == true)
{
ViewData["Events"] = data;
}
else if (showPrevious)
{
ViewData["Events"] = data.Where(u => u.ShowPrevious == true).ToList();
}
else if(showUpcoming)
{
ViewData["Events"] = data.Where(u => u.ShowUpcoming == true).ToList();
}
return View();
}
}
3. 컨트롤러 :
public class HomeController : Controller
{
public IActionResult ReloadEvents(bool showPrevious, bool showUpcoming)
{
return ViewComponent("Events", new { showPrevious = showPrevious, showUpcoming = showUpcoming });
}
public IActionResult Index()
{
var model = new Event() { ShowPrevious = true, ShowUpcoming = true };
return View(model);
}
}
4. Index.cshtml :
@model Event
@Html.CheckBoxFor(m => m.ShowPrevious, new { id = "cbShowPrevious", onchange = "ReloadEvents()" })
@Html.CheckBoxFor(m => m.ShowUpcoming, new { id = "cbShowUpcoming", onchange = "ReloadEvents()" })
<div id="events">
@await Component.InvokeAsync("Events", new { showPrevious = Model.ShowPrevious, showUpcoming = Model.ShowUpcoming })
</div>
@section Scripts
{
<script>
function ReloadEvents() {
$.ajax({
url: '@Url.Action("ReloadEvents", "Home")',
data: {
showPrevious: document.getElementById("cbShowPrevious").checked,
showUpcoming: document.getElementById("cbShowUpcoming").checked
},
success: function (data) {
$("#events").html(data);
}
})
}
</script>
}
5. Default.cshtml (뷰 구성 요소 Razor 뷰) :
@model Event
@{
List<Event> events = ViewData["Events"] as List<Event>;
if (events.Count > 0)
{
<table>
<tr>
<th>ShowPrevious</th>
<th>ShowUpcoming</th>
<th>Data</th>
</tr>
<tbody>
@foreach (var item in events)
{
<tr>
<td>@item.ShowPrevious</td>
<td>@item.ShowUpcoming</td>
<td>@item.Data</td>
</tr>
}
</tbody>
</table>
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다