asp mvc 5将表值传递给控制器

时间:2016-06-01 12:34:00

标签: asp.net-mvc-4 json.net

如何通过json发送数据写入数据库?

    @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>

如何获取控制器中的值?任何人都可以建议课程,教材吗?

我的控制器:

public class AdminController : Controller
{
    EfDbContext context = new EfDbContext();
    [HttpPost]
    public JsonResult AddSchedule(string data)
    {
        return Json("Все нормально!" + data);
    }
}

我还想问一下,如果没有json响应中的属性TableID,可以将数据存储在数据库中吗?

1 个答案:

答案 0 :(得分: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...
        },
    });
});

Razor ;

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