Tuesday 2 August 2016

dynamic page in MVC


@model Nop.Admin.Models.QuestionnaireManagement.QuestionnaireManagementVM

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="formDetails" style="border: none; padding:0; margin:0">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="form-group">
                @Html.LabelFor(model => model.Questions, htmlAttributes: new { @class = "" })<span class="mandatory"><b>*</b></span>
                @Html.HiddenFor(m => m.Id)
                @Html.HiddenFor(m => m.Type)
                @Html.HiddenFor(m => m.CreatedBy)
                @Html.HiddenFor(m => m.CreatedDate)
                @Html.HiddenFor(m => m.UpdatedBy)
                @Html.HiddenFor(m => m.UpdatedDate)
                @Html.HiddenFor(m => m.Mark)
                @Html.TextAreaFor(model => model.Questions, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Questions, "", new { @class = "text-danger", @id = "QuestionsError" })

            </div>
        </div>
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="form-group">
                @Html.LabelFor(model => model.ControlTypeId, htmlAttributes: new { @class = "" })<span class="mandatory"><b>*</b></span>
                @Html.DropDownListFor(model => model.ControlTypeId, Model.ControlType, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ControlTypeId, "", new { @class = "text-danger", @id = "ct" })

            </div>
        </div>

        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="form-group">
                @Html.LabelFor(model => model.StatusId, htmlAttributes: new { @class = "" })<span class="mandatory"><b>*</b></span>
                @Html.DropDownListFor(model => model.StatusId, Model.Status, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StatusId, "", new { @class = "text-danger", @id = "st" })

            </div>
        </div>
        <div class="col-md-12 col-sm-12 col-xs-12" id="ValueDiv">
            <div class="form-group">
                <div class="col-md-7 zero-padding-left">
                    <label>Value</label>
                </div>

                <div class="col-md-3 ">
                    <a href="#" id="btnPlus"><img class="img-responsive" src="/Administration/Content/images/plus-icon.png"></a>
                </div>
                <div class="col-md-12 zero-padding-left c">

                    <div class="col-md-7 zero-padding-left">
                        <input type="text" class="form-control value" />
                    </div>
                    <div class="col-md-3 btn-css">
                        <a href="#" name="del"><img class="img-responsive" src="/Administration/Content/images/minus-icon.png"></a>
                    </div>
                </div>
            </div>
        </div>

        <div class="clearfix"></div>
        <div class="options k-button-add marginTopbtn">
            <input type="button" value="Save" class="k-button  btn-default padd-left-right" id="btnSubmit" />
            <input type="button" class="k-button k-button-cancel disabled padd-left-right" value="Cancel" id="btnCancel">
        </div>
    </div>
}

<style>
    .btn-css img {
        padding-top: 10px;
        padding-bottom: 10px;
        padding-left: 8px;
    }
</style>
<script type="text/javascript">
    $(function () {
        var row;
        $('#ValueDiv').hide();
        if ($('#ControlTypeId').val() == 1) {
            $('#ValueDiv').show();
        }
        else
            $('#ValueDiv').hide();
        $('#ControlTypeId').change(function () {
            if ($('#ControlTypeId').val() == 1) {
                $('#ValueDiv').show();
            }
            else
                $('#ValueDiv').hide();
        });
        row = $('.c').clone();
        $('#btnPlus').click(function () {

            var crow = row.clone();
            $('#ValueDiv').append(crow);
        });
        $('body').on('click', "a[name='del']", function () {
            var row = $(this).parents('.c');
            if ($('.c').length > 1)
                row.remove();
        });
        if(@Model.Mark==2 && @Model.ControlTypeId==1)
        {
            var data=@Html.Raw(Json.Encode(@Model.Values));
            $('#ValueDiv').find('.c').remove();
            for(var i=0;i<data.length;i++)
            {
                $('#ValueDiv').last().append("<div class='col-md-12 zero-padding-left c'><div class='col-md-7 zero-padding-left'><input type='text' value='"+data[i]+"' class='form-control value'/></div><div class='col-md-3 btn-css'><a href='#' name='del'><img class='img-responsive' src='/Administration/Content/images/minus-icon.png'></a></div></div>");
            }
        }
        $('#btnSubmit').click(function () {
            $('#QuestionsError').text('');
            $('#ct').text('');
            $('#st').text('');
            var check = 0;
            if ($('#Questions').val() == "") {
                $('#QuestionsError').text('Questions should not be blank.');
                $('#Questions').focus();
                check = 1;
            }
            if ($('#ControlTypeId').val() == 0) {

                $('#ct').text('Please select a control type.');

                check = 1;

            }
            if ($('#StatusId').val() == 0) {
                $('#st').text('Please select a active.');

                check = 1;
            }
            if (check == 1)
                return false;
            var marr = [];
            $.each($('.c'), function (i, j) {
                marr.push($(j).find('.value').val());
            });
            var questionnaireManagementVM = {
                Id: $('#Id').val(),
                Type: $('#Type').val(),
                CreatedBy: $('#CreatedBy').val(),
                CreatedDate: $('#CreatedDate').val(),
                UpdatedBy: $('#UpdatedBy').val(),
                UpdatedDate: $('#UpdatedDate').val(),
                Mark: $('#Mark').val(),
                Questions: $('#Questions').val(),
                ControlTypeId: $('#ControlTypeId').val(),
                StatusId: $('#StatusId').val(),
                Values: marr
            }
            $.ajax({
                cache: false,
                type: "Post",
                url: "/QuestionnaireManagement/AddQuestion",
                data: { questionnaireManagementVM: questionnaireManagementVM },
                success: function (data) {
                    location.reload(true);
                }
            });
        });

        $('#btnCancel').click(function () {
            var window = $('#FillAddQuestion');
            window.data('kendoWindow').close();
        })
    });
</script>

No comments:

Post a Comment