本文介绍了如何在按钮点击时将HTML数据发布到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着从html视图classAttandanceList到控制器获取数据的问题



当我点击复选框时它无法正常工作如何得到真实假我当检查它



我尝试了什么:



i am facing problem about getting data from html view classAttandanceList to controller

when i click on checkbox it is not working properly how to get get true false when i check it

What I have tried:

引用:

我的代码如下:



型号



my Code is given below

Model

 public partial class Tab_Attendance
    {
        [Key]
        public int Attendance_Id { get; set; }

        public DateTime? Atten_Date { get; set; }

        public int? Class_Id { get; set; }

        public int? Student_Id { get; set; }

        public int? Section_Id { get; set; }

        public int? Teacher_Id { get; set; }

        public string Atten_Status { get; set; }
    }
    //public class MustBeTrueAttribute : ValidationAttribute
    //{
    //    public override bool IsValid(object value)
    //    {
    //        return value is bool && (bool)value;
    //    }
    //}
    public class ClassAttendanceList

    {
        public bool CheckStatus { get; set; }

        //public bool Selected { get; set; }
        public int? Class_Id { get; set; }

        public string Name { get; set; }

        public string ClassName { get; set; }

        public string SecName { get; set; }
        public int? SecId { get; set; }
        public int studid { get; set; }



    }
}







----查看---








----View---


@model IEnumerable<Faheem.Models.ClassAttendanceList>

@{
    ViewBag.Title = "ClassAttendance";
}

<<h1 class="page-header"> Student Attendance</h1>
<!-- end page-header -->
<!-- begin row -->
<div class="row">
    <div class="col-md-8 ui-sortable">
        <!-- begin panel -->
        <div class="panel panel-inverse" data-sortable-id="form-stuff-1">
            <div class="panel-heading">
                <div class="panel-heading-btn">
                    <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-default" data-click="panel-expand"><i class="fa fa-expand"></i></a>
                    <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-success" data-click="panel-reload"><i class="fa fa-repeat"></i></a>
                    <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-warning" data-click="panel-collapse"><i class="fa fa-minus"></i></a>
                    <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-danger" data-click="panel-remove"><i class="fa fa-times"></i></a>
                </div>
                <h4 class="panel-title">Creat Class Attendance</h4>
            </div>
            <div class="panel-body">
                @using (Html.BeginForm())
                            {
                                @Html.AntiForgeryToken()

                                <div class="form-horizontal">

                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                        <div class="form-group">
                            @Html.Label("Slect Class", new { @class = "col-sm-4 control-label bold" })
                            <div class="col-md-8">
                                @*@Html.EditorFor(model => model.Campus_Id, new { htmlAttributes = new { @class = "form-control" } })*@
                                @Html.DropDownList("Class_Id", null, htmlAttributes: new { @class = "form-control" })

                            </div>

                        </div>
                        <br />

                        <div class="row">
                            <div class="col-md-12">
                                <table id="tblattendance" class="table table-striped table-bordered dataTable no-footer dtr-inline">
                                     <thead>
                                        <tr role="row">

                                            <th>

                                                    @*<input type="checkbox" data-group-cls="btn-group-justified" checked class="checkbox" value="true" name="chej">*@

                                            </th>

                                            <th  class="productth">Name</th>
                                            <th  class="productth">ClassName</th>
                                            <th  class="productth">SecName</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        @*<tr>
                                            <td>
                                                <input type="checkbox" data-group-cls="btn-group-justified" checked class="checkbox" value="true" name="CheckStatus">
                                                <input type="hidden" value="false" name="CheckStatus" />
                                            </td>
                                        </tr>*@
                                        @*@if (Model != null)
                                        {

                                            foreach (var item in Model.Item2)
                                            {

                                        <tr>
                                            <td>

                                                @Html.DisplayFor(modelItem => item.Class_Id)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => ite)
                                            </td>

                                        </tr>



                                            }
                                        }*@

                                    </tbody>
                                </table>
                            </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-8 col-md-offset-4">
                                    <input type="submit" value="Save" class="btn btn-success m-r-5 m-b-5" />
                                    <a href="/Attendance/ClassAttedance" class="btn btn-default m-r-5 m-b-5"> Cancel </a>
                                </div>
                            </div>
                        </div>
                        }
                    </div>
        </div>
    </div>
   </div>


 <script>

     $(document).ready(function () {

         $("#Class_Id").change(function () {


             $("#tblattendance tbody tr").remove();

             $.ajax({

                 //url: '/Attendance/ClassAttendance',
                 // type: "GET",
                 // dataType: "JSON",
                 type: 'GET',

                 url: '@Url.Action("GetClass")',
                 dataType: 'json',
                 data: { Classid: $("#Class_Id").val() },
                 success: function (data) {
                     var items = '';
                     $.each(data, function (i, item) {

                         var rows ="<tr>"
                         + "<td> <input id='" + item.CheckStatus + "' name='CheckStatus' data-group-cls='btn-group-justified' checked='checked' class='checkbox' value='true' type='checkbox' /></td>"
                         + "<td class='prtoducttd'>" + item.Name + "</td>"
                         + "<td class='prtoducttd'>" + item.ClassName + "</td>"
                         + "<td class='prtoducttd'>" + item.SecName + "</td>"
                         + "</tr>";
                         $('#tblattendance tbody').append(rows);
                     });

                 },
                 error: function (ex) {
                     var r = jQuery.parseJSON(response.responseText);
                     alert("Message: " + r.Message);
                     alert("StackTrace: " + r.StackTrace);
                     alert("ExceptionType: " + r.ExceptionType);
                 }
             });
             return false;
         })
     });






 </script>

@*<script>

    $('.btn-success').on("click", function () {
        var tr = $(this).parents('tr:first');
        var id = tr.find("#Class_Id").val();
        var name = tr.find(".Name").val();
        var clasname = tr.find(".ClassName").val();
        var isSuccess = -1;

        var LIST =
        {
            "Class_Id": id,
            "Name": name,
            "ClassName": clasname
        };

        $.ajax({

            // HomeController and InsertData method

            url: '/Attendance/insertdata/',

            data: JSON.stringify(LIST),

            type: 'POST',

            contentType: 'application/json; charset=utf-8',

            success: function (result) {
                isSuccess = result;

            },
            error: function (result) {
                isSuccess = result;
            }

        }).done(function () {
            if (isSuccess == "1") {           // Successfully saved
                tr.find('.edit, .display').toggle();
                alert("Successfully Saved");
                location.reload();      // refresh the page
            }
            else {                      // Data Error
                alert("Error. Please, check the data");
            }
        });
    });
</script>*@
   @*<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(function () {
            PrepareCheckbox();
        });
        function PrepareCheckbox(){
            document.getElementById("checkbox").CheckStatus = true;
        }
</script>*@







--Cotroller---












--Cotroller---




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Faheem.Models;

namespace Faheem.Controllers
{
    public class AttendanceController : Controller
    {
        // GET: Attendance
        SIMSModel Sims = new SIMSModel();
        public ActionResult ClassAttendance()
        {

            ViewBag.Class_Id = new SelectList(Sims.Tab_Class.ToList(), "Class_Id", "ClassName");
            //ViewBag.Checkbox_Id = new SelectList(Enumerable.Empty<SelectListItem>());
            //if (Session["Tab_Attendance"] != null)
            //{
            //    var t1 = new Tuple<IEnumerable<ClassAttendanceList>, Tab_Attendance>((IEnumerable<ClassAttendanceList>)Session["Tab_Attendance"], null);

            //    return View(t1);
            //}

            return View();
        }

        public ActionResult GetClass(int Classid = 0)
        {
            if (Classid == 0)
            {
                RedirectToAction("Index");

            }
            Session["Class_id"] = Classid;
            String Query = "EXEC SP_ClassAttendanceList " + Classid;
            var ClassAttendancelist = Sims.Database.SqlQuery<ClassAttendanceList>(Query);
            return Json(ClassAttendancelist, JsonRequestBehavior.AllowGet);
        }
        //[HttpPost] 

        //public JsonResult InsertData(ClassAttendanceList inStudent)
        //{
        //    String result = String.Empty;
        //    // var Student = new SelectList(Sims.Tab_Student.Where(m => m.Class_Id == Class).ToList()
        //    // var a=new SelectList(Sims.cl).Where(m=>m)
        //    //  Models.ClassAttendanceList dup = ClassAttendanceList.Find(p =>p == inStudent.ID);
        //    String Query = " EXEC SP_ClassAttendanceList " + inStudent.Class_Id;
        //    var ClassAttendancelist = Sims.Database.SqlQuery<ClassAttendanceList>(Query);

        //    foreach (var item in ClassAttendancelist)
        //    {
        //        item.ClassName = inStudent.ClassName;

        //    }
        //    //if (dup == null)
        //    //{
        //    //    ClassAttendanceList.Add(inStudent);
        //    //    result = "1";
        //    //}
        //    //else
        //    //{
        //    //    result = "0";
        //    //}

        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        [HttpPost]
        public ActionResult ClassAttendance([Bind(Include = "Attendance_Id,Atten_Date,Class_Id,Student_Id,Section_Id,Teacher_Id,Atten_Status")] Tab_Attendance at , ClassAttendanceList clist )

        {

            // model.CheckStatus = true;
            String Query = " EXEC SP_ClassAttendanceList " + Session["Class_id"];
            var list = Sims.Database.SqlQuery<ClassAttendanceList>(Query);

            foreach (var item in list)
            {

                at.Class_Id = int.Parse(item.Class_Id.ToString());
                at.Student_Id = int.Parse(item.studid.ToString());
                at.Section_Id = int.Parse(item.SecId.ToString());
               // at.Atten_Status = "P";

                using (var context = new SIMSModel())
                {
                  //  bool CheckStatus = Convert.ToBoolean(coll["CheckStatus"]);

                    if (clist.CheckStatus == true)
                    {
                        string Query1 = "EXEC SP_AttendanceInsertUpdate " + at.Class_Id + "," + at.Student_Id + "," + at.Section_Id + "," + "'" + "P" + "'";
                        context.Database.ExecuteSqlCommand(Query1);
                    }

                    else {

                        string Query1 = "EXEC SP_AttendanceInsertUpdate " + at.Class_Id + "," + at.Student_Id + "," + at.Section_Id + "," + "'" + "A" + "'";
                        context.Database.ExecuteSqlCommand(Query1);


                    }

                }



            }


            return View();
        }

    }

        }













<pre>










推荐答案




这篇关于如何在按钮点击时将HTML数据发布到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:16