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

Model item value in Grid popup EditorTemplate

2 Answers 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 24 Apr 2014, 05:58 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 28 Apr 2014, 12:34 PM
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.

 
0
Stephen
Top achievements
Rank 1
answered on 01 May 2014, 08:21 PM
Hi Daniel,

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

Thanks!

Steve
Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Stephen
Top achievements
Rank 1
Share this question
or