This is a migrated thread and some comments may be shown as answers.

[Solved] Need multiple PartialViews in one aspx

3 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
IQworks
Top achievements
Rank 1
IQworks asked on 25 Dec 2009, 01:00 AM
 Hi,
    I am new to telerik MVC ....
    I have a view that does a render partialview .....  
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Work.Master"%> 
 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
 <div> 
     <h1>MemberNo: <%=ViewData["vdMemberNo"]%></h1>   
     <h1>Body Composition Analysis for :  <%= TempData["vdDisplayName"]%></h1>  
 </div>    
</asp:Content> 
 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
    <p> 
       Telerik GRID Data .....  
    </p> 
  <table>   
  <tr> 
  <td align="center" style="width:800px;" > 
    
  <% Html.RenderPartial("MemberGrid", Model); %> 
    
  </td>    
  </tr> 
      
  </table>    
     
    </asp:content> 
 

    The partial view for the above looks like this....
<%@ Control Language="C#"   
Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MBSAnalysisMVC.Models.Members>>" %>     
     
<%  Html.Telerik().Grid(Model)  
    .Name("MemberGrid")  
    .Columns(columns => 
    {  
        columns.Add(o => o.MemberNo).Width(10);  
     columns.Add(o => o.Username).Width(150);  
     columns.Add(o => o.FirstName).Width(150);  
     columns.Add(o => o.DateCreated).Format("{0:MM/dd/yyyy}").Width(120);  
      columns.Add(c =>  {  
                        %> 
                        <%= Html.ActionLink("Edit", "Edit", new { id = c.MemberNo },  
                                 new { @class="t-link action-edit" })%>   
                        <%= Html.ActionLink("Delete", "Delete", new { id = c.MemberNo },  
                            new { @class = "t-link action-delete" }) %> 
                        <%   
                        }).Title("Actions").Width(150);     
                      })  
       // .Ajax() // Needs additional information.  
          .Scrollable()  // Will throw off position, needs to be further configured.  
        .Filterable()  
       //.Pageable()  
        .Sortable()  
        .Render();  
        %> 
      <style type="text/css">   
         .action-edit { background-image: url('<%= ResolveUrl("~/Content/Images/Icons/edit.png") %>'); }  
         .action-delete { background-image: url('<%= ResolveUrl("~/Content/Images/Icons/delete.png") %>'); }   
         .action-edit, .action-delete      
         {padding-left: 18px;  
          background-repeat: no-repeat;  
          background-position: 0 50%;  
          margin-right: 10px;}  
          .action-edit:hover,  
          .action-delete:hover  
          {text-decoration: none;}  
      </style>    
       <p> 
        <%= Html.ActionLink("Create New", "Create") %> 
    </p>    
    
 
 The controller that runs them both looks like this .... 
 public ActionResult Management()  
        {  
           ViewData["iqMemberNo"] = TempData["vdMemberNo"].ToString();  
             
           ViewData["orientation"] = "Horizontal"; // "Vertical";  
           MBSAnalysisMVCEntities _dataModel = new MBSAnalysisMVCEntities();  
             return View(_dataModel.Members.ToList());  
          // return View();// displays the paritalview, but it is empty.   
        } 

   How can I just call the view from my controller like this ..... 
  return View();   
   Not really sure of the most effecient way for this Partial View to read the  "Members" Model and populate its Telerik Grid ?  Is it a FOR loop that I need in the Parital View ? If so, is there documentation someplace that tells me how to populate the columns and rows ?
   All of the telerik examples I have seen, populate the views by passing data from the controller ?

  Thanks for your time.
 

3 Answers, 1 is accepted

Sort by
0
IQworks
Top achievements
Rank 1
answered on 25 Dec 2009, 01:26 AM

Sorry, the question does not match the title, let me explain this a little better. .
    I have 3 Partial views that I want to display in the one aspx. But back to the previous posts, I can only send one Model from the controller to this view with 

 

return

 

View(_dataModel.Members.ToList());
But I have two more Partial Views that will be on that aspx page that is being called. So, I would need to pass the following as well 
_dataModel.Company.ToList());
_dataModel.Facility.ToList());
This means that in the controller, I would  also need to somehow return View for these other two, which wont work (Or I dont know how to send three of these at once). .  

This is why I thought it would be better to just populate the data inside each of the 3 Partial view ascx's. 
  So, I wanted to know how to just call 
  return View(), and when the aspx is called and does a RenderPartial for each Partial View... 

 

<% Html.RenderPartial(

"MemberGrid", Model); %>

 

 

 

<% Html.RenderPartial(

"CompanyGrid",Model ); %>

 

 

 

<% Html.RenderPartial(

"FacilityGrid", Model); %>

 

 

   the CompanyPartialView.ascx would populate its own Grid data from the Company Model, 
   the FacilityPartialView.ascx would populate its own Grid data from the Facility Model, 
  and the  MemberPartialView.ascx would populate its Grid from the Member Model.    
  without passing any Model data from the controller. 
  So,  I would like to just do ....
  return View(). .From the controller.  

thanks again.
 

0
IQworks
Top achievements
Rank 1
answered on 27 Dec 2009, 02:12 AM
 after a little research, I think I may be asking for a way to break the MVC "Concerns Rule" ?
 Loading data within a view should be discouraged. Data should be comming from the controller. 
 So this makes me wonder if MVC might be TO strict ?
 It makes sense to me that if you can have a Partial view user control that loads its own data, that this is encapsulation of a sort. And that this is a good thing. It makes the Partial view self contained, a true user control.
 Since I have a choice, this is the way I am going to go.

 If I am not looking at this the wrong way, I would love to hear about it so I can learn something.

 In the meantime, I still am looking for how to load an MVC telerik grid within a usercontrol Partial view  without passing the data from a controller via the ViewData or TempData ?

  thanks.
0
Atanas Korchev
Telerik team
answered on 28 Dec 2009, 11:53 AM
Hi IQworks,

You can use the following code to configure the grid:

Partial View:

Html.Telerik.Grid((IEnumerable<MBSAnalysisMVC.Models.Members>)ViewData["Members"])

Controller:

MBSAnalysisMVCEntities _dataModel = new MBSAnalysisMVCEntities();
ViewData["Members"] =_dataModel.Members.ToList();

Populating components from ViewData is the recommended way to do it in ASP.NET MVC. However it is still possible to bind the grid entirely from the user control:

<%
MBSAnalysisMVCEntities _dataModel = new MBSAnalysisMVCEntities();

Html.Telerik.Grid(_dataModel.Members.ToList())
              /*rest of grid configuration*/

%>

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
IQworks
Top achievements
Rank 1
Answers by
IQworks
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or