Model item value in Grid popup EditorTemplate

3 posts, 1 answers
  1. Stephen
    Stephen avatar
    158 posts
    Member since:
    Jan 2009

    Posted 24 Apr 2014 Link to this post

    I am trying to conditionally disable fields in a grid popup editortemplate based on a value in the passed in Model.  The value is correct in the model if I use an Html helper like a hidden field, but if I try to access it using razor code it is not there.  I assume this has to do with the order of how things are loading but I'mm not sure how to fix it.  Here is the editortemplate code:

    @model PASSAdmin.ViewModels.ResourceReviewer.ResourceReviewViewModel
     
    @{
        bool disabled = true;  
        if (Model.Status == "BLREV")
        {
            disabled = false;
        }
    }
     
    @Html.HiddenFor(model => model.Beamline_Request_ID, Model.Beamline_Request_ID)
    @Html.HiddenFor(model => model.Status, Model.Status)
     
    <div class="editor-container" style="width:700px;">
     
        <p>Please provide your recommendation of the experiment described in this Beam Time Request with regard to feasibility and safety.</p>
     
        <div>
            <div class="editor-label">
                @Html.Label("Approve")
            </div>
            <div class="editor-field">
                @if (disabled)
                {
                    @Html.RadioButtonFor(model => model.Refused_By_Beamline, "N", new { disabled = true })
                }
                else
                {
                    @Html.RadioButtonFor(model => model.Refused_By_Beamline, "N")
                }
            </div>
     
            <div class="editor-label">
                @Html.Label("Deny")
            </div>
            <div class="editor-field">
                @if (disabled)
                {
                    @Html.RadioButtonFor(model => model.Refused_By_Beamline, "Y", new { disabled = true })
                }
                else
                {
                    @Html.RadioButtonFor(model => model.Refused_By_Beamline, "Y")
                }
            </div>
        </div>
     
        <br class="clear" />
        <br />
        <br />
     
        <p>By selecting Approve you are signifying the Experiment is feasible and can be performed safely on the indicated Beamline. If you select to Deny any of the above, please provide an explanation in the Comments area below. In addition to any specific comments, it is suggested you make note of any particular beamline equipment to be used during this experiment.  Other staff may not have access to the full proposal information, but will have access to these comments.</p>
     
        @*
        <div class="editor-label">
            @Html.Label("Comments")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Refused_By_Beamline, "N")
            @Html.ValidationMessageFor(model => model.Refused_By_Beamline)
        </div>
        *@         
        <p>Note: comments entered here are visible to the Principal Investigator</p>
     
    </div>

    When I view the source I can see that the hidden for Model.Status has the correct value but when I try to set the razor variable so I can use it to display the fields disabled, it does not have the value at all.

    What am I missing? 








  2. Answer
    Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 28 Apr 2014 Link to this post

    Hello Stephen,

    This will occur if you are using Ajax editing. In this case the editor is serialized once on the server with a model that has the default values and after that is reused on the client. In this case I can suggest one of the following approaches in order to disable the input based on a model value:
    • Include a property in the model that indicates if the inputs should be disabled and use disabled binding:
      @Html.RadioButtonFor(model => model.Refused_By_Beamline, "N", new { data_bind="disabled:DisabledFieldName"})
    • Use the grid edit event to disable the inputs when needed:
      function edit(e) {
          if (e.model.Status == "BLREV") {
              $("#Refused_By_Beamline").prop('disabled', true);
          }
      }


    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Stephen
    Stephen avatar
    158 posts
    Member since:
    Jan 2009

    Posted 01 May 2014 in reply to Daniel Link to this post

    Hi Daniel,

    I used your first option and it is working for me.

    Thanks!

    Steve
Back to Top