2013-04-12 4 views
1

MVC4 및 Entity Framework를 사용하여 wep 앱을 개발 중입니다. 저의 견해 중 하나에 모든 세부 사항이있는 사람들의 목록이 있습니다. 처음에는 이름과 성만 표시되지만 링크를 통해 세부 정보를 표시 할 수 있으므로 가능한 한 동적으로 만들고 싶습니다.MVC 4 비동기 작업 호출

내 작업은 Json(object, JsonRequestBehavior.AllowGet)

어떤 생각들을 반환?

편집 : 내 행동

 public ActionResult ListByOwner(long id) 
    { 
     var productallocations = db.ProductAllocations.Where(obj => obj.Id_Person == id).Include("Product"); 
     return PartialView(productallocations); 
    } 

내보기 :

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>@ViewBag.Title - My ASP.NET MVC Application</title> 
     <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
     <meta name="viewport" content="width=device-width" /> 
     @Styles.Render("~/Content/css") 
     @Styles.Render("~/Content/bootstrap") 
     @Scripts.Render("~/bundles/modernizr") 
     <script src="../../Scripts/globalize.js" type="text/javascript"></script> 
     <script src="../../Scripts/globalize.culture.fr-FR.js" type="text/javascript"></script> 
    </head> 
    <body> 
     <div class="navbar navbar-inverse navbar-fixed-top"> 
      <div class="navbar-inner"> 
      <div class="container-fluid"> 
       <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       </button> 
       <a class="brand" href="@Url.Action("Index", "Home")">BuSI Material Manager</a> 
       <div class="nav-collapse collapse"> 
       <p class="navbar-text pull-right"> 
        Logged in as @User.Identity.Name 
       </p> 
       <ul class="nav"> 
        <li>@Html.ActionLink("Home","Index","Home")</li> 
        <li class="dropdown"> 
         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Persons <b class="caret"></b></a> 
         <ul class="dropdown-menu"> 
          <li>@Html.ActionLink("All persons list","Index","Person")</li> 
          <li>@Html.ActionLink("All allocations list", "Index", "ProductAllocation")</li> 
         </ul> 
        </li> 
        <li class="dropdown"> 
         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Cards<b class="caret"></b></a> 
         <ul class="dropdown-menu"> 
          <li>@Html.ActionLink("All phone cards list", "Index", "PhoneCard")</li> 
          <li>@Html.ActionLink("All fuel cards list", "Index", "VehicleFuelCard")</li> 
         </ul> 
        </li> 
        <li class="dropdown"> 
         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Products <b class="caret"></b></a> 
         <ul class="dropdown-menu"> 
          <li>@Html.ActionLink("All products list", "Index", "Product")</li> 
          <li>@Html.ActionLink("Products in stock", "ProductStock", "Product")</li> 
          <li>@Html.ActionLink("Allocated products", "AllocatedProducts", "Product")</li> 
          <li class="divider"></li> 
          <li>@Html.ActionLink("Product types", "Index", "ProductType")</li> 
          <li>@Html.ActionLink("Product companies", "Index", "ProductCompany")</li> 
         </ul> 
        </li> 
        <li class="dropdown"> 
         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Vehicles <b class="caret"></b></a> 
         <ul class="dropdown-menu"> 
          <li>@Html.ActionLink("All vehicles list", "Index", "Vehicle")</li> 
          <li>@Html.ActionLink("Available vehicles", "AvailableVehicles", "Vehicle")</li> 
          <li>@Html.ActionLink("Allocated vehicles", "AllocatedVehicles", "Vehicle")</li> 
          <li class="divider"></li> 
          <li>@Html.ActionLink("Vehicle types", "Index", "VehicleType")</li> 
          <li>@Html.ActionLink("Vehicle motor types", "Index", "VehicleMotorType")</li> 
          <li class="divider"></li> 
          <li><a href="@Url.Action("Index", "VehicleInsuranceContract")">Insurance contracts</a></li> 
          <li><a href="@Url.Action("Index", "VehicleLeasingContract")">Leasing contracts</a></li> 
         </ul> 
        </li> 
       </ul> 
       </div> 
      </div> 
      </div> 
     </div> 

     <div id="body"> 
      @RenderSection("featured", required: false) 
      @RenderBody() 
     </div> 

     <footer> 
      <p>Developed by me.</p> 
     </footer> 

     @Scripts.Render("~/bundles/jquery") 
     @Scripts.Render("~/bundles/jqueryval") 
     @Scripts.Render("~/bundles/bootstrap") 
     @RenderSection("scripts", required: false) 
     <script src="../../Scripts/Utils.js" type ="text/javascript"></script> 
    </body> 

</html> 

번들 :

public class BundleConfig 
    { 
     // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725 
     public static void RegisterBundles(BundleCollection bundles) 
     { 
      bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
         "~/Scripts/jquery-{version}.js")); 

      bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
         "~/Scripts/jquery-ui-{version}.js")); 

      bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
         "~/Scripts/jquery.unobtrusive*", 
         "~/Scripts/jquery.validate*")); 

      // Use the development version of Modernizr to develop with and learn from. Then, when you're 
      // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. 
      bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
         "~/Scripts/modernizr-*")); 

      bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); 

      bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
         "~/Content/themes/base/jquery.ui.core.css", 
         "~/Content/themes/base/jquery.ui.resizable.css", 
         "~/Content/themes/base/jquery.ui.selectable.css", 
         "~/Content/themes/base/jquery.ui.accordion.css", 
         "~/Content/themes/base/jquery.ui.autocomplete.css", 
         "~/Content/themes/base/jquery.ui.button.css", 
         "~/Content/themes/base/jquery.ui.dialog.css", 
         "~/Content/themes/base/jquery.ui.slider.css", 
         "~/Content/themes/base/jquery.ui.tabs.css", 
         "~/Content/themes/base/jquery.ui.datepicker.css", 
         "~/Content/themes/base/jquery.ui.progressbar.css", 
         "~/Content/themes/base/jquery.ui.theme.css")); 
     } 
    } 
+1

jquery를 사용하여 ajax 요청을 한 다음 json 결과에서 html을 빌드하여 페이지에 삽입 할 수 있습니다. 개인적으로 액션을 부분 뷰를 반환하도록 만들면 jquery는 html을 받고 페이지에 직접 삽입 할 수 있습니다. –

답변

0

어떤 생각들? 부분을 ​​반환 컨트롤러 액션이

<div id="details"></div> 

마지막 :

예, 당신은 AJAX 링크를 사용할 수 있습니다

@Ajax.ActionLink(
    "Details", 
    "Details", 
    new { id = person.Id }, 
    new AjaxOptions { UpdateTargetId = "details" } 
) 

를 다음 세부 사항이 표시됩니다 자리가 JSON 대신보기 :

public ActionResult Details(int id) 
{ 
    var model = repository.Get(id); 
    return PartialView(model); 
} 

해당 부분보기 당신은 세부 사항을 할 수 :

@model Person 
<div> 
    @Html.DisplayFor(x => x.FirstName) 
</div> 
<div> 
    @Html.DisplayFor(x => x.LastName) 
</div> 
<div> 
    @Html.DisplayFor(x => x.Age) 
</div> 
... 

비고 : 당신이 Ajax.ActionLink 작업 할 경우 _Layout에서 jquery.unobtrusive-ajax.js 스크립트를 포함하는 것을 잊지 마세요.

+0

고마워, 나는 그것을 시도 할 것이다. – Traffy

+0

도움을 주셔서 감사합니다. 그러나 여전히 작동하지 않습니다. 나는 당신이 만든 것을 따라 갔고 ActionLink는 나를 다른보기로 리디렉션하려고합니다 (그래서 오류가 표시됩니다). 내 레이아웃에 올바른 파일을 포함 시켰습니다. – Traffy

+0

**'jquery.unobtrusive-ajax.js' 전에'jquery.js' **를 포함 시켰는지 확인하십시오. 또한 ASP.NET MVC 4를 사용하는 경우 동일한 스크립트를 여러 개 포함하지 않으려면 번들을 사용할 때 조심해야합니다. –