1

Is it possible to do this in the razor View . I want to inject below JavaScript only if statement is true else not. How can i do this.

            @if (Model.EmployeeInterviews != null)
            {
                $("#select-Candidate")[0].selectize.setValue(@Html.Raw(JsonConvert.SerializeObject(Model.CandidateId)));
                var employee = @Html.Raw(JsonConvert.SerializeObject(selectedempobj));
                var val = "[";
                for (i = 0; i < employee.length; i++)
                {
                    val += employee[i].EmployeeID + ",";
                }
                val = val.substring(0, val.length - 1) + "]";

                $("#select-Employee")[0].selectize.setValue(JSON.parse(val));
                $('#txtScheduledOn').val('@String.Format("{0:M/d/yyyy HH:mm tt}", Model.ScheduledOn)');
                $('#txtCompletedOn').val('@String.Format("{0:M/d/yyyy HH:mm tt}", Model.CompletedOn)');
                $('#hdnEmployeeId').val(JSON.parse(val).toString());
                $('#hdnCandidateId').val('@Html.Raw(JsonConvert.SerializeObject(Model.CandidateId))');
            }
1
  • 1
    Try to wrap your JS <text></text> tags Commented Nov 6, 2014 at 12:00

2 Answers 2

9

Something like this

<script type="text/javascript">
    $(document).ready(function () {
        //... some common javascript

        @{
            if (Model.EmployeeInterviews != null) {
            <text>
            $("#select-Employee")[0].selectize.setValue(JSON.parse(val));
            $('#txtScheduledOn').val('@String.Format("{0:M/d/yyyy HH:mm tt}", Model.ScheduledOn)');
            //... specific javascript
            </text>
            }
        }
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

Try the following

@if (Model.EmployeeInterviews != null)
{
    <script type="text/javascript">
        $("#select-Candidate")[0].selectize.setValue(@Html.Raw(JsonConvert.SerializeObject(Model.CandidateId)));
        @{
            IHtmlString employee = @Html.Raw(JsonConvert.SerializeObject(selectedempobj));
         }
        string val = "[";
        for (int i = 0; i < employee.length; i++)
        {
            val += employee[i].EmployeeID + ",";
        }
        val = val.substring(0, val.length - 1) + "]";

        $("#select-Employee")[0].selectize.setValue(JSON.parse(val));
        $('#txtScheduledOn').val('@String.Format("{0:M/d/yyyy HH:mm tt}", Model.ScheduledOn)');
                $('#txtCompletedOn').val('@String.Format("{0:M/d/yyyy HH:mm tt}", Model.CompletedOn)');
                $('#hdnEmployeeId').val(JSON.parse(val).toString());
                $('#hdnCandidateId').val('@Html.Raw(JsonConvert.SerializeObject(Model.CandidateId))');
    </script>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.