Nice programing

asp.net MVC 부분보기 컨트롤러 작업

nicepro 2020. 10. 16. 08:06
반응형

asp.net MVC 부분보기 컨트롤러 작업


저는 웹 앱 개발이 처음이고 최신 기술로 시작할 것이라고 생각했기 때문에 asp.net과 MVC 프레임 워크를 한 번에 배우려고 노력하고 있습니다. 이것은 아마도 MVC 전문가 여러분에게 매우 간단한 질문입니다.

내 질문은 부분보기에 관련 작업이 있어야하며, 그렇다면 일반 페이지가 RenderPartial()부분보기에서 사용할 때마다이 작업이 호출 됩니까?


부분보기를 반환하는 작업이있을 수 있지만 부분보기를 렌더링하기위한 작업은 필요하지 않습니다. RenderPartial은 부분 뷰를 가져 와서 주어진 모델과 뷰 데이터 (제공된 경우)를 사용하여 현재 (상위) 뷰로 렌더링합니다.

AJAX를 사용하여 페이지의 일부를로드 / 다시로드하는 경우 부분보기를 반환하는 작업이 필요할 수 있습니다. 이 경우 페이지의 일부만 다시로드하기를 원하므로 전체보기를 반환하는 것은 바람직하지 않습니다. 이 경우 페이지의 해당 섹션에 해당하는 부분보기 만 반환하도록 작업을 수행 할 수 있습니다.

표준 메커니즘

일반보기 내에서 부분보기 사용 (작업 필요 없음)

...some html...
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
...more html..

Ajax 메커니즘

AJAX를 통해 페이지의 일부를 다시로드 (부분은 초기 페이지로드에서 인라인으로 렌더링 됨)

...some html...
<div id="partial">
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
</div>
...more html...

<script type="text/javascript">
   $(function() {
       $('#someButton').click( function() {
           $.ajax({
              url: '/controller/action',
              data: ...some data for action...,
              dataType: 'html',
              success: function(data) {
                 $('#partial').html(data);
              },
              ...
           });
       });
   });
</script>

AJAX 용 컨트롤러

public ActionResult Action(...)
{
     var model = ...

     ...

     if (Request.IsAjaxRequest())
     {
          return PartialView( "Partial", model.PartialModel );
     }
     else
     {
          return View( model );
     }
}

허용되는 대답은 완전히 정확하지만 jQuery로드를 사용하여 부분보기를로드 할 수 있다고 추가하고 싶습니다. 동시성을 고려하지 않으려면 필요한 구성이 적습니다.

$("#Your-Container").load("/controller/action/id");

The answer is no. But sometimes you need some controller action behind a partial view. Then you can create an actionMethod wich returns a partial view. This actionMethod can be called within another view:

@Html.Action("StockWarningsPartial", "Stores")

The actionmethod can look like:

public ActionResult StockWarningsPartial()
{
      ....              
      return View("StockWarningsPartial", warnings);

}

and the view 'StockWarningsPartial.cshtml' starts with:

@{
    Layout = null;
}

to make it not render your surrounding layout again.


I was able to achieve something similar with this logic.

Within the .cshtml

@Html.Action("ActionMethodName", "ControllerName");

Within the controller

[Route("some-action")]
public ActionResult ActionMethodName()
{
    var someModel = new SomeModel();
    ...
    return PartialView("SomeView.cshtml", someModel);
}

And that's it.

If you need to pass values from the .cshtml to the action method then that is possible to.

참고URL : https://stackoverflow.com/questions/1371031/asp-net-mvc-partial-view-controller-action

반응형