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

时间:2014-09-17 10:51:52

标签: c# asp.net asp.net-mvc-5

我有一个评估视图,其中包含一个学生填充表格的表格。该表有两个单独的文本框添加到每行

这是评估视图

@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,将其发布到另一个模型调用具有学生的结果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();
    }

这就是我提出的......请不要确定我是否在正确的轨道上请任何想法......

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法:

在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();
    }