绑定表行文本框和Id,然后保存到数据库

本文关键字:然后 保存 数据库 Id 文本 绑定 | 更新日期: 2023-09-27 18:22:20

我有一个评估视图,其中有一个由特定类填充的学生表。该表有两个单独的文本框,分别添加到行

这是评估视图

@model  _5._3_EduAdmin.Models.AssessmentViewModel
@using (Html.BeginForm("Assessment", "Student", FormMethod.Post)){

@Html.HiddenFor(m => m.ClassId)
<div class="row-fluid">
    <div class="span8 offset2">
        <div class="box">
            <div class="box-content nopadding">
                <table class="table table-hover table-nomargin table-condensed table-striped">
                    <thead>
                        <tr>
                            <th>#
                            </th>
                            <th>Students Name</th>
                            <th>Assessment Score</th>
                            <th>Exam Score</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in ViewData["Students"] as IEnumerable<_1._2._0_Core.Domain._2._10_Domain.StudentManagement._10._1_StudentModels.Student>)
                        {
                            int serial = 0;
                            serial++;
                            <tr>
                                <td>@serial</td>
                                <td>@item.Surname, @item.FirstName @item.MiddleName</td>
                                <td class="span3">
                                    <div class="controls">
                                        @Html.TextBoxFor(m => m.TestAggregate, new {@class = "input-medium"})
                                    </div>
                                </td>
                                <td class="span3">
                                    <div class="controls">
                                        @Html.TextBoxFor(m => m.ExamsScore, new {@class = "input-medium"})
                                    </div>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
}

现在,我想将每个学生id绑定到该行的文本框中,这样当发布到数据库时,我就可以将该特定分数分配给数据库中的学生id,并将其发布到另一个模型调用Result中,该模型中有学生id参考。

我如何让它在我的控制器中以这种方式工作,然后将它插入到我的服务中,将其保存到数据库中。

这是我的控制器

[HttpPost]
    public ActionResult Assessment(AssessmentViewModel model)
    {

            var students = _studentService.getNurStudentbyClass(model.ClassId).Select(x => x.Id);
            foreach (var id in students)
            {
                if (model.ExamsScore == 0)
                {
                    if (model.TestAggregate != 0)
                    {
                        model.ExamsScore = 0;
                        model.TotalScore = model.TestAggregate + model.ExamsScore; 
                        _recordService.CreateResult(model.TestAggregate,model.ExamsScore,model.ExamsScore,model.Grade,model.TermId,model.SubjectId,id,model.SessionId);
                    }
                }
            }
        }
        return View();
    }

这就是我想到的。。。请不确定我是否在正确的轨道上请任何想法。。。

绑定表行文本框和Id,然后保存到数据库

您可以使用以下方法:

在mvc中使用jquery ajax。使用输入参数的id收集值,并根据各自的id通过ajax发送。

 Change your html as below:  
                $(document).ready(function(){
                $(".Update").click(function(){
                    var allValues=$(input[name:allValues]);
                    var arrId = [];
                    var arrExamsScore = [];
                    var arrTestAggregate = [];
                                $.each(allValues, function(i, item) { 
                                   arrId.push($(this).val())
                                   arrTestAggregate.push($(this).attr("data-TestAggregate"))
                                   arrExamsScore.push($(this).attr("data-ExamsScore"))
                                });

                    $.ajax({
                            url:"ControllerName/Assessment",
                            data:{"Id":Id.join(","),"ExamsScore":ExamsScore.join(","),"TestAggregate":TestAggregate.join(",")}
                            success:function(result){
                            }
                    });
                }); 
            })  
                @foreach (var item in ViewData["Students"] as IEnumerable<_1._2._0_Core.Domain._2._10_Domain.StudentManagement._10._1_StudentModels.Student>)
                {
                    int serial = 0;
                    serial++;
                    <tr>
                        <td>@serial</td>
                        <td>@item.Surname, @item.FirstName @item.MiddleName</td>
                        <td class="span3">
                            <div class="controls">
                                @Html.TextBoxFor(m => m.TestAggregate, new {@class = "input-medium",@id="TestAggregate"+@item.Id})
                            </div>
                        </td>
                        <td class="span3">
                            <div class="controls">
                                @Html.TextBoxFor(m => m.ExamsScore, new {@class = "input-medium",@id="ExamsScore"+@item.Id}) 
                                <input type="hidden" name="allValues" value="@item.Id" data-ExamsScore="@item.ExamsScore" data-TestAggregate="@item.TestAggregate"/>
                            </div>
                        </td>
                    </tr>
                }

现在按照以下更改控制器的第一行

    [HttpPost]
    public ActionResult Assessment(string Id,string ExamsScore,string TestAggregate) 
    {        /// split here above input values 
        /// convert to int and then process

            var students = _studentService.getNurStudentbyClass(Id).Select(x => x.Id);
            foreach (var id in students)
            {
                if (model.ExamsScore == 0)
                {
                    if (model.TestAggregate != 0)
                    {
                        model.ExamsScore = 0;
                        model.TotalScore = model.TestAggregate + model.ExamsScore; 
                        _recordService.CreateResult(model.TestAggregate,model.ExamsScore,model.ExamsScore,model.Grade,model.TermId,model.SubjectId,id,model.SessionId);
                    }
                }
            }
        }
        return View();
    }