2016-06-14 1 views
0

사용자가 데이터베이스에 반영해야하는 것처럼 클릭 한 즉시 확인란 상태를 저장하려고합니다. 여기 MVC - 클릭하자마자 HTML 도우미 확인란의 상태를 저장하거나 버튼 하나를 클릭하여 저장하십시오.

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

    <table id="table_id" class="display"> 
     <thead> 
      <tr> 
       <th> 
        ID 
       </th> 
       <th> 
        Category ID 
       </th> 
       <th> 
        Category Type 
       </th> 
       <th> 
        Reported By 
       </th> 
       <th> 
        Actioned 
       </th> 
       @*<th> 
         Edit 
        </th>*@ 
      </tr> 
     </thead> 
     @foreach (var item in Model) 
     { 
      <tbody> 
       <tr> 
        <td> 
         @Html.DisplayFor(modelItem => item.ID) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.CategoryID) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.CategoryType) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.ReportedBy) 
        </td> 
        <td> 
         @Html.CheckBoxFor(model => item.Actioned) 
        </td> 
       </tr> 
      </tbody> 
     } 
    </table> 

    <input type="submit" value="Save" class="btn btn-default" /> 

} 

Snap of the output screen

, Html.CheckBoxFor @는 (모델 => item.Actioned)

데이터베이스의 필드에서 조치 유형 비트이다.

그래서 나는 아예 모든 체크 박스의 상태를 저장 2. 사용자가 클릭해야 1. 체크 박스 상태가되는 즉시 또는 다른 을 클릭으로 데이터베이스에 반영되어야한다, 하나의 버튼을 저장을 시도하고 중 변경됩니다.

다른 세부 정보가 필요한 경우 알려 주시기 바랍니다.

+1

(그것은 너무 HTML 헬퍼를 사용하여 수행 할 수 있습니다) 데이터베이스, 당신은 서버에 요청을해야합니다. 이상적으로, 특정 요소의 값이 변경 될 때이를 수행하면 AJAX 요청으로이를 수행합니다. "저장"버튼을 클릭해도 정상적인 양식 게시는 괜찮을 것입니다. 너 뭐 해봤 니? – David

+0

부수적으로'foreach' 루프 안에서'@ Html.CheckBoxFor()'를 사용하면 작동하지 않습니다 - [this answer] (http://stackoverflow.com/questions/30094047/html-table-to -ado-net-datatable/30094943 # 30094943) –

+0

안내를 위해 @David에게 감사드립니다. 나는 AJAX 호출을 시도했다. 나는 비슷한 게시물 [여기]에서 기사를 보았다 (http://stackoverflow.com/questions/10687538/mvc-pass-value-to-controller-via-js).이 기사는 정말 나를 도왔습니다. – user3201573

답변

-2
function ChangeStatus(CheckedReportedSpamID) { 
    $.ajax({ 
     url: "/ReportedSpam/UpdateCheckedStatus/", 
     type: 'POST', 
     data: { ReportedSpamID: CheckedReportedSpamID } 
    }); 
} 

내가 HTML 앵커 태그를 사용한 MVC 새로운 오전 이후에 아무것도 유지하기 위해

<input type="checkbox" id="chkActioned" onclick="ChangeStatus('@row.ItemArray[0]')" /> 

Controller.cs

[HttpPost] 
    public ActionResult UpdateCheckedStatus(string ReportedSpamID) 
    { // use the ID which is passed and do something like Actioned = !Actioned; } 
관련 문제