2016-06-01 3 views
-1

json을 통해 데이터를 보내 데이터베이스에 쓰려면 어떻게해야합니까?asp mvc 5 컨트롤러에 테이블 값 전송

@model Diploma.WebUI.Models.ScheduleListViewModel 
@{ 
    Layout = "~/Views/Shared/_AdminLayout.cshtml"; 
    ViewBag.Title = "Успеваемость"; 
} 

    <form method="POST"> 
     <div class="table table-responsive myTable"> 
      <table class="table bellTable scheduleTable sidebar-wrapper"> 
       <thead> 
       <tr class="top"> 
        <th>&nbsp;</th> 
        <th> 
         <span style="position: relative;"> 
          </span> 
        </th> 
        <th></th> 
        <th> 
         <span style="position: relative;"></span> 
        </th> 
        <th class="classroom" colspan="4">ДиКТ-31-12 - четная неделя</th> 
        <th class="classroom" colspan="4">ДиКТ-31-12 - нечетная неделя</th> 
       </tr> 
       <tr> 
        <th>День</th> 
        <th>№</th> 
        <th>Начало</th> 
        <th>Подгруп</th> 
        <th>Тип</th> 
        <th>Предмет</th> 
        <th>Преподаватель</th> 
        <th class="classroom">Аудитория</th> 
        <th>Тип</th> 
        <th>Предмет</th> 
        <th>Преподаватель</th> 
        <th class="classroom">Аудитория</th> 
       </tr> 
       </thead> 
       <tbody> 
       @foreach (var line in Model.Weeks) 
       { 
        <tr class="1"> 
         <td class="day heading" rowspan="9" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(177, 181, 186); display: table-cell;">@line.DayWeek</td> 
         @{ 
          foreach (var number in @Model.NumberLessons) 
          { 
           <tr> 
            <td class="lessonNumber heading" rowspan="1" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(177, 181, 186);">@number.NumberLessonId.ToString()</td> 
            <td class="time heading" rowspan="1" style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(177, 181, 186);">@number.NumberLessonTime</td> 
            <td class="lessonSub"> 
             <span class="addSub">[ + ]</span> 
             <span class="deleteSub" style="display: none;">[ - ]</span> 
            </td> 
            <td class="position" style="display: none">1</td> 
            <td class="row type class24855even" valign="top"> 
             @Html.DropDownList("type", Model.TypeLessons as SelectList, 
              htmlAttributes: new {@class = "input"}) 
            </td> 
            <td class="row subject class24855even"> 
             @Html.DropDownList("discipline", Model.Disciplines as SelectList, 
              htmlAttributes: new {@class = "input"}) 
            </td> 
            <td class="row teacher class24855even"> 
             @Html.DropDownList("teacher", Model.Teachers as SelectList, 
              htmlAttributes: new {@class = "input"}) 
            </td> 
            <td class="row classroom class24855even"> 
             @Html.DropDownList("classroom", Model.Classrooms as SelectList, 
              htmlAttributes: new {@class = "input"}) 
            </td> 
            <td class="row type class24855odd"> 
             <input class="editor" type="text" tabindex="-1" value=""> 
            </td> 
            <td class="row subject class24855odd"> 
             <input class="editor" type="text" tabindex="-1" value=""> 
            </td> 
            <td class="row teacher class24855odd"> 
             <input class="editor" type="text" tabindex="-1" value=""> 
            </td> 
            <td class="row classroom class24855odd"> 
             <input class="editor" type="text" tabindex="-1" value=""> 
            </td> 
           </tr> 
          } 
         } 
        </tr> 
       } 
       </tbody> 
      </table> 
     </div> 
     <input type="submit" value="Сохранить" class="btn btn-default"/> 
    </form> 

컨트롤러의 값은 어떻게 받습니까? 누구나 수업을 제안하고 교재를 제안 할 수 있습니까?

내 컨트롤러 :

나는 또한 JSON 응답의 속성 테이블 ID없이 데이터베이스에 데이터를 저장할 수 있습니다 물어보고 싶은게
public class AdminController : Controller 
{ 
    EfDbContext context = new EfDbContext(); 
    [HttpPost] 
    public JsonResult AddSchedule(string data) 
    { 
     return Json("Все нормально!" + data); 
    } 
} 

?

답변

0

으로 할 수 있습니다. jQuery;

var postdata = { "data": your_data }; 

$.ajax({ 
     type: "POST", 
     url: "../Admin/AddSchedule", 
     dataType: "json", 
     data: JSON.stringify(postdata), 
     contentType: 'application/json; charset=utf-8', 
     success: function() { 
      //Do something on success... 
     }, 
    }); 
}); 

또는 면도기와;

@using (Html.BeginForm("AddSchedule", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post)){ 
    // Your form html here... 
} 
+0

Html.BeginForm을 사용할 때 @foreach 문제가 발생하면 Week 모델이 즉시 사라집니다. –

관련 문제