referencing the current enumerated object of the gird, editor templates

7 posts, 0 answers
  1. Paul
    Paul avatar
    1 posts
    Member since:
    Aug 2011

    Posted 16 Jul 2013 Link to this post

    Hi, 
        I am trying to render a different partial view based on a property of the model. I will then need to pass the model from the current enumeration to the Html.RenderPartial called in the template 

    as below i need to pass the current model from the edit to the RenderPartial i.e. "themodel".  How do I get the syntax to pass the model to it?

    Thanks 

    @using Assessments.Models;
    @model IEnumerable<FallsAssessment>
     
    @{
        ViewBag.Title = "FallsAssessmentList";
    }
     
    <h2>FallsAssessmentList</h2>
    @Html.Kendo().Grid(Model).Name("FallsAssessments").Columns(columns=>
        {
            columns.Bound(m => m.AssessmentType).Title("Assessment Type");
            columns.Bound(m=> m.Ward).Title("Ward");
            columns.Bound(m=>m.LastUpdated_TS).Title("Last Updated");
            columns.Bound(m => m.LastUpdatedBy).Title("User");
            columns.Bound(m => m.Score);
            columns.Command(m => m.Edit().Text("Edit"));
        }
        ).ToolBar(t=>t.Create()).DataSource(d=>d.Ajax()
            .Model(
                model=>
                    {
                        model.Id(id=>id.EventID);
                        model.Field(field=>field.EventID).Editable(false);
                    }).Create("Create", "FallsAssessment").Update("Update", "FallsAssessment")).Editable(e => e.Mode(GridEditMode.PopUp).TemplateName("edit-template"))
                   
     
                  FallsAssessmentFallsAssessmentFallsAssessment
     
    <script id="edit-template" type="text/x-kendo-template">
    #if(AssessmentType=='Child'){#
    @Html.RenderPartial("AdultAssessment",themodel)
    #}else{#
    @Html.RenderPartial("AdultAssessment", themodel)
    #}
    </script>
  2. David
    David avatar
    6 posts
    Member since:
    Jun 2013

    Posted 17 Jul 2013 Link to this post

    Hi, is there any update to this? is it possible to do?
  3. Daniel
    Admin
    Daniel avatar
    2226 posts

    Posted 18 Jul 2013 Link to this post

    Hello,

    The model cannot be passed to the PartialView helper when using Ajax binding. The helper is executed on the server and the model is loaded on the client via Ajax. In order to pass the model you should use a server detail template. Another option would be to load the content via Ajax in the detailInit event or with a widget that supports loading the content via Ajax as the tabstrip. An alternative solution would be to use the template syntax to populate the model values in the partial views.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  4. David
    David avatar
    6 posts
    Member since:
    Jun 2013

    Posted 18 Jul 2013 Link to this post

    The server templates look ideal.. but the issue is I can't see how to use them for an edit item...  Is it possible?

    currently I am having to build up my own custom ajax link to post to the controller to render the view I wan't in a custom modal popup.... I don't like it as the Ajax html helpers don't work correctly in the grid and it all has to be done with strings. 



  5. Daniel
    Admin
    Daniel avatar
    2226 posts

    Posted 22 Jul 2013 Link to this post

    Hello,

    Sorry, I misunderstood the previous question. If the partial view should be used for editing then you should use server editing and use the conditional logic in the EditorTemplate:

    @model MyModel
     
    @if(Model.Field)
    {
        Html.RenderPartial("FirstPartial", Model);
    }
    else
    {
        Html.RenderPartial("SecondPartial", Model);   
    }
    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. David
    David avatar
    6 posts
    Member since:
    Jun 2013

    Posted 23 Jul 2013 Link to this post

    Hi Daniel, 
                      Yes a new that I could create a template just to have the logic on which partial view to display.  At the time of posting I could see that people were accessing the properties of the model in the teplating javascript syntax. So I was hoping not to have to follow that and use a view/EditorTemplate just for a conditional statement. 

    I settled for a custom ajax action link to call a controlled which passed back the correct partial view to display, and rendered it in a bootstrap modal. 

    It in hindsight creating the custom server template as you suggested may have saved some time with the java script I needed. 

    I will be happy to mark this as answered as I think we have covered all the scenarios to get this working.

    Thanks

    David  
  7. Daniel
    Admin
    Daniel avatar
    2226 posts

    Posted 25 Jul 2013 Link to this post

    Hello David,

    In the editor template it is possible to bind the elements in order to access the value when using Ajax editing and for popup editing, it is also possible to access the values with a template expression(${} in this case). In both cases however, the values will be available on the client and so a server side method cannot be used.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top