Friday 12 August 2016

Dynamic page in MVC


Controller Code :
 public JsonResult GetQuestion(int Type)
        {
            return Json(_questionnaireManagementService.GetAllQuestionnaireManagement(0, int.MaxValue, Type).Where(n => n.StatusId == 1).OrderBy(p => p.Id).Select(m => new { m.Id, Question = m.Questions, ControlType = m.ControlTypeId, Value = _questionnaireManagementService.GetAllControlTypeMapping(m.Id).Select(n=>n.Value) }), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult PostAnswer(int equipmentId, List<AnswerList> lst, string status,int sid,int wid,string fromDate,string fromTime,string toDate,string toTime)
        {
            try
            {
                Test test = new Test();
                test.EquipmentId = equipmentId;
                test.Status = status;
                test.PerformedBy = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.GetFullName();
                test.TestDate = DateTime.UtcNow;
                _testService.InsertTest(test);
                var m = _testService.GetTests();
                if (m.Count == 0)
                    TestId = 1;
                else
                    TestId = (m.OrderByDescending(t => t.Id)).ToList()[0].Id;
                _testDetailsService.InsertTestDetails(TestId, lst);
                EquipmentLocation equipmentLocation = new EquipmentLocation();
                equipmentLocation.EquipmentId = equipmentId;
                Equipment equipment = _equipmentService.GetEquipmentAsPerId(equipmentId);
                equipmentLocation.ModelId = equipment.ModelID;
                equipmentLocation.ManufacturerId = equipment.ManufacturerID;
                equipmentLocation.EquipmentStatusID = sid;
                equipmentLocation.IsWarehouse = true;
                equipmentLocation.WarehouseId = wid;
                equipmentLocation.FromDate = Convert.ToDateTime(fromDate);
                equipmentLocation.FromTime = fromTime;
                equipmentLocation.ToDate = Convert.ToDateTime(toDate);
                equipmentLocation.ToTime = toTime;
                equipmentLocation.IsDeleted = false;
                int id = _equipmentLocationService.InsertEquipmentLocation(equipmentLocation);
                TempData["isFromBarcodeScanning"] = true;
                return Json("1",JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
         
        }

View Code :
<form id="frmBenchTest" name="frmBenchTest">
    <div class="form-group">

        <div id="dvbench" class="content">
            <input type="hidden" id="EQID" />
            <table id="tb" border="0" cellpadding="0" cellspacing="0" class="adminContent table-responsive benchQustable table-striped"></table>
            <div class="options submitStyle">
                <input type="button" value="Pass" class="k-button padd-left-right" id="btnSave" />
                <input type="button" id="btnCancel007" value="Fail" class="k-button k-button-cancel padd-left-right" />
            </div>

        </div>
    </div>
</form>
java script code :
For open.
<script>
$('#tb').empty().append('<thead class="k-grid-header" ><tr class="" ><th class="k-header" style="width:80%;"><input type="hidden" name="WID"/><input type="hidden" name="FD"/><input type="hidden" name="FT"/><input type="hidden" name="TD"/><input type="hidden" name="TT"/>Question</th><th class="k-header" style="width:20%;">Answer Type</th></tr></thead>');
            $.ajax({
                url: '/Barcode/GetQuestion',
                type: 'Get',
                data: { Type: 3 },
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].ControlType == 1)
                        {
                            var x = '';
                            $('#tb').append('<tr><td>' + data[i].Question + '</td><td><input type="hidden" name="hf" value="' + data[i].Id + '" /><input type="hidden" name="hfct" value="' + data[i].ControlType + '" /><select name="ddl"><option value="0">Select</option>' + $.each(data[i].Value, function (m, n) {
                               x=x+ '<option value="'+n+'">'+n+'</option>'
                            })
                                +''+x+'</select><span class="field-validation-error" name="sps"></span></td></tr>');
                        }
                        if (data[i].ControlType == 2)
                            $('#tb').append('<tr><td>' + data[i].Question + '</td><td><input type="hidden" name="hf" value="' + data[i].Id + '" /><input type="hidden" name="hfct" value="' + data[i].ControlType + '" /><input type="text" name="txt"  class="form-control"><span class="field-validation-error"  name="spt"></span></td></tr>');
                        if (data[i].ControlType == 3)
                            $('#tb').append('<tr><td>' + data[i].Question + '</td><td><input type="hidden" name="hf" value="' + data[i].Id + '" /><input type="hidden" name="hfct" value="' + data[i].ControlType + '" /> <textarea name="txtarea"  class="form-control"></textarea><span class="field-validation-error"  name="spta"></span></td></tr>');
                        if (data[i].ControlType == 4)
                            $('#tb').append('<tr class="k-alt" ><td>' + data[i].Question + '</td><td><input type="hidden" name="hf" value="' + data[i].Id + '" /><input type="hidden" name="hfct" value="' + data[i].ControlType + '" /><input type="text" name="txtc" ><span  name="spc" class="field-validation-error"></span></td></tr>');
                        if (data[i].ControlType == 5)
                            $('#tb').append('<tr class="k-alt" ><td>' + data[i].Question + '</td><td><input type="hidden" name="hf" value="' + data[i].Id + '" /><input type="hidden" name="hfct" value="' + data[i].ControlType + '" /><input type="text" name="txttime" ><span  name="spct" class="field-validation-error"></span></td></tr>');

                    }


                }
            });
 $('#PerformBenchTest').on('click', '#btnPerformBenchTestSave', function () {
                    var mark = 0;
                    $('#WarehouseIdm').text('');
                    $('#FromDatem').text('');
                    $('#FromTimem').text('');
                    $('#Todatem').text('');
                    if ($('#WarehouseId').val() == 0)
                    {
                        $('#WarehouseIdm').text('Please select a warehouse');
                        mark = 1;
                    }
                    if ($('#StartDate').val() == "")
                    {
                        $('#FromDatem').text('From date should not be blank.');
                        mark = 1;
                    }
                    if ($('#StartTime').val() == "")
                    {
                        $('#FromTimem').text('From time should not be blank.');
                        mark = 1;
                    }
                    if (Date.parse($('#StartDate').val()) > Date.parse($('#EndDate').val()))
                    {
                        alert('sankar');
                        $('#Todatem').text('From_date should be less than to_date.');
                        mark = 1;
                    }
                    if (mark == 1)
                        return false;
                    else
                    {
                        var window007 = $('#BenchTestdv');
                        if (!window007.data('kendoWindow')) {

                            window007.kendoWindow({
                                modal: true,
                                title: 'Bench Test Questionnaire',
                                actions: ['Close'],
                                scrollable: true,
                                draggable: false,
                                resizable: false,
                                visible: false,
                            });
                            $('#BenchTestdv').parent('.k-window').addClass('responsivewidth-Bench');
                        }
                        $('#frmBenchTest').trigger("reset");
                        $('.field-validation-error').empty();
                        $('.field-validation-valid').empty();
                        window007.data('kendoWindow').center().open();
                        $('#tb').find('tr').each(function (i, j) {
                            $(j).find('td').find("input[name=txtc]").kendoDatePicker();
                        });
                        $('#tb').find('tr').each(function (i, j) {
                            $(j).find('td').find("input[name=txttime]").kendoTimePicker({ format: "HH:mm" });
                        });
                        $('#tb').find('tr th input[name=WID]').val($('#WarehouseId').val());
                        $('#tb').find('tr th input[name=FD]').val($('#StartDate').val());
                        $('#tb').find('tr th input[name=FT]').val($('#StartTime').val());
                        $('#tb').find('tr th input[name=TD]').val($('#EndDate').val());
                        $('#tb').find('tr th input[name=TT]').val($('#EndTime').val());
                    }
            });
        });

</script>
For save
<script type="text/javascript">
    $(function () {
        var Wid, fd, ft, td, tt;
        function save(s, Sid) {
            var answer = "";
            var datasave = [];
            var mark = 0;
            $('#tb').find('tr td').find("[name='spt']").text('');
            $('#tb').find('tr td').find("[name='spc']").text('');
            $('#tb').find('tr td').find("[name='sps']").text('');
            $('#tb').find('tr td').find("[name='spta']").text('');
            $('#tb').find('tr td').find("[name='spct']").text('');
            $('#tb').find('tr').each(function (i, j) {
                if (i != 0) {
                    if ($(j).find('td').find("input[name='txt']").val() == "") {
                        $(j).find('td').find("[name='spt']").text('This field should be not blank.');
                        mark = 1;
                    }
                    if ($(j).find('td').find("[name='txtarea']").val() == "") {
                        $(j).find('td').find("[name='spta']").text('This field  should not be blank.');
                        mark = 1;
                    }
                    if ($(j).find('td').find("input[name='txtc']").val() == "") {
                        $(j).find('td').find("[name='spc']").text('This date field should not be blank.');
                        mark = 1;
                    }
                    if ($(j).find('td').find("input[name='txttime']").val() == "") {
                        $(j).find('td').find("[name='spct']").text('This time field should not be blank.');
                        mark = 1;
                    }
                    if ($(j).find('td').find("select[name='ddl']").val() == 0) {
                        $(j).find('td').find("[name='sps']").text('Please select a answer from the dropdown.');
                        mark = 1;
                    }
                    if ($(j).find('td').find("input[name='hfct']").val() == 1)
                        answer = $(j).find('td').find("select[name='ddl']").val();
                    else if ($(j).find('td').find("input[name='hfct']").val() == 2)
                        answer = $(j).find('td').find("input[name='txt']").val();
                    else if ($(j).find('td').find("input[name='hfct']").val() == 3)
                        answer = $(j).find('td').find("[name='txtarea']").val();
                    else if ($(j).find('td').find("input[name='hfct']").val() == 4)
                        answer = $(j).find('td').find("[name='txtc']").val();
                    else if ($(j).find('td').find("input[name='hfct']").val() == 5)
                        answer = $(j).find('td').find("[name='txttime']").val();
                    datasave.push({ QuestionId: $(j).find('td').find("input[name='hf']").val(), ANSWER: answer });
                }
            });

            if (mark == 1)
                return false;
            else {

                $.ajax({
                    url: '/Barcode/PostAnswer',
                    data: { equipmentId: $('#landingPageEquipmentId').val(), lst: datasave, status: s, sid: Sid, wid: Wid, fromDate: fd, fromTime: ft, toDate: td, toTime: tt },
                    type: 'Post',
                    success: function (data) {
                        if (data == 1) {
                            $('#frmBenchTest').trigger("reset");
                            $('.field-validation-error').empty();
                            $('.field-validation-valid').empty();
                            window.location.href = "/Admin/Equipment/ListLocation/" + $('#landingPageEquipmentId').val();
                        }
                        else
                            alert(data);

                    }
                });
            }
        }

        $('#btnSave').click(function () {

            Wid = $('#tb').find('tr th').find("[name='WID']").val();
            fd = $('#tb').find('tr th').find("[name='FD']").val();
            ft = $('#tb').find('tr th').find("[name='FT']").val();
            td = $('#tb').find('tr th').find("[name='TD']").val();
            tt = $('#tb').find('tr th').find("[name='TT']").val();
            save('Bench test Successful', 12);

        });
        $('#btnCancel007').click(function () {
            Wid = $('#tb').find('tr th').find("[name='WID']").val();
            fd = $('#tb').find('tr th').find("[name='FD']").val();
            ft = $('#tb').find('tr th').find("[name='FT']").val();
            td = $('#tb').find('tr th').find("[name='TD']").val();
            tt = $('#tb').find('tr th').find("[name='TT']").val();
            save('Bench test Failed', 11);
        });
    });
</script>






No comments:

Post a Comment