2017-06-06 36 views
0

我想要的功能在下面的圖像:顯示數據庫表中的行並將其發佈到另一個表。選擇行返回1和未選定的行返回0到本狀態列

image

實際上欲一個HTML表包括複選框中顯示從數據庫表中的數據在每一行旁邊並將這些數據存儲到另一個表中。檢查數據和未選中兩者都將被存儲,但檢查數據返回1和未檢查數據在is_selected列中返回0。我想知道這個程序。我無法理解我是如何做到的。這裏添加了一張圖片。如果你看到它,你會完全明白我想要做什麼。我正在使用ms sql server 2014。和我的項目asp.net-MVC(5)的Visual Studio 2017感謝ü
查看

@using AttendenceSystem.Models; 
@model IEnumerable<CsteY4_T2_Students> 

@{ 
    ViewBag.Title = "Multimedia Communication Students"; 
} 

<div style="font-family:Arial"> 

    <h2>Multimedia Communication Students</h2> 
    <form> 

     <table border="1" id="tblList"> 
      <tr> 
       <th>Student Id</th> 
       <th>Student Name</th> 
       <th>Present Status</th> 
      </tr> 

      @foreach (CsteY4_T2_Students cstey4t2studens in @Model) 
      { 
       <tr> 
        <td class="num">@cstey4t2studens.StudentId</td> 
        <td class="num">@cstey4t2studens.StudentName</td> 
        <td> 
         <input name="chkStudent" value="@cstey4t2studens.StudentId" type="checkbox" /> 
        </td> 
       </tr> 

      } 

     </table> 
     <input type="submit" id="btnSave" class="addValue" value="Save" /> 
    </form> 
</div> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 

    <script type="text/javascript"> 

     $('.addValue').click(tallyValues); 

    function tallyValues() { 

      $('#btnSave').on('click', function() { 
       var list = []; 
       $('input[name="chkStudent"]').each(function() { 
        console.log($(this).val()); 
        if ($(this).is(':checked')) { 
         list.push({ 'StudentId': $(this).val(), 'PresentValid': true }) 
        } 
        else { 
         list.push({ 'StudentId': $(this).val(), 'PresentValid': false }) 
        } 
       }); 
       $.ajax({ 
        type: "POST", 
        url: "../CsteY4T2Students/SaveStudent", 
        data: { list: list }, 
        cache: false, 
        success: function (data) { 
         console.log('saved'); 
        }, 
        error: function (error) { 
         console.log('error'); 
        }, 
        complete: function() { 

        } 
       }); 
      }); 
    }; 
</script> 

控制器代碼:

 public class CsteY4T2StudentsController : Controller 
     { 
      // GET: CsteY4T2Students 
      public ActionResult Cstey4t2students(string SubjectCode) 
      { 
       DataRetrive dataretrive = new DataRetrive(); 
       List<CsteY4_T2_Students> cstey4t2students=dataretrive.Cstey4t2studens.Where(sc => sc.SubjectCode == SubjectCode).ToList(); 
       return View(cstey4t2students); 

      } 
      public JsonResult SaveStudent(List<Student> list) 
      { 
       return Json(1, JsonRequestBehavior.AllowGet); 
      } 
     } 

模型類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace AttendenceSystem.Models 
{ 
    [Table("tbleMultimediaCommunication")] 
    public class Student 
    { 
     [Key] 

     public int StudentId { get; set; } 
     public bool PresentValid { get; set; } 
    } 
} 
+0

你有什麼已經嘗試過?請發佈您在實現上述功能時遇到的特定問題。 – User3250

+0

其實我想顯示數據從一個html表格中的數據庫表格,包括每一行旁邊的複選框,並將這些數據存儲到另一個表中檢查數據和未選中兩者都將被存儲,但檢查數據返回1和未檢查數據在is_selected列中返回0。我想知道這個程序。我無法理解我是如何做到的。這裏添加了一張圖片。如果你看到它,你會完全明白我想要做什麼。謝謝 – Ashraf

+0

我把你的所有代碼都正確地設計好了,根據你的理念設計數據庫。但無法理解哪裏(腳本在視圖中)的代碼將過去以及我如何編寫代碼?有沒有任何Java腳本或jQuery和ajax參考需要? SaveStudent.cshtml中(url:「../CsteY4T2Students/SaveStudent」)的代碼是什麼?謝謝 – Ashraf

回答

0

基於我在下站在這裏是一個樣本,還沒有測試,但應該帶你正確的方向:

您的視圖 - 新增表ID,看看複選框

<table border="1" id="tblList"> 
    <tr> 
    <th>Student Id</th> 
    <th>Student Name</th> 
    <th>Present Status</th> 
    </tr> 
    @foreach (CsteY4_T2_Students cstey4t2studens in @Model) 
    { 
    <tr> 
    <td>@cstey4t2studens.StudentId</td> 
    <td>@cstey4t2studens.StudentName</td> 
    <td> 
     <input name="chkStudent" value="@cstey4t2studens.StudentId" type="checkbox" /> 
    </td> 
    </tr> 
    } 
</table> 
<button type="button" id="btnSave">Save</button> 

腳本中查看

$(function() { 

    $('#btnSave').on('click', function() { 
     var list = []; 
     $('input[name="chkStudent"]').each(function() { 
      console.log($(this).val()); 
      if ($(this).is(':checked')) { 
       list.push({ 'StudentId': $(this).val(), 'PresentValid': true }) 
      } 
      else { 
       list.push({ 'StudentId': $(this).val(), 'PresentValid': false }) 
      } 
     }); 
     $.ajax({ 
      type: "POST", 
      url: "../CsteY4T2Students/SaveStudent",    
      data: { list: list }, 
      cache: false, 
      success: function (data) { 
       console.log('saved'); 
      }, 
      error: function (error) { 
       console.log('error'); 
      }, 
      complete: function() { 

      } 
     }); 
    }); 
}); 

在控制器

public JsonResult SaveStudent(List<Student> list) 
    { 
     return Json(1, JsonRequestBehavior.AllowGet); 
    } 

假設你有StudentClass如下:

class Student 
{ 
    public int StudentId {get;set;} 
    public bool PresentValid {get;set} 
} 
相關問題