使用getJSON显示列表

时间:2015-09-16 11:25:58

标签: json asp.net-mvc-4

我正试图在我的视图中获取中心列表。这是我的代码:

ListExamens.cshtml:

 <script type="text/javascript">
    function EditExamen(id) {
        $.getJSON('/Parametrage/result?examenId=' + id), function (data) {
            $.each(data, function (key, val) {
                 $('#IntituleM').text(val.name);  
            });
        };
    }
</script>

@foreach (var item in Model.Item1)
{
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Intitule)</td>
        <td>@Html.DisplayFor(modelItem => item.Direction.Description)</td>
        <td>@Html.DisplayFor(modelItem => item.Datelimite)</td>
        <td>
            <a class="btn btn-custom btn-xs" href="javascript:EditExamen(@item.Id)" title="Editer">
                <i class="fa fa-edit"></i>
            </a>
            <a class="btn btn-custom btn-xs" href="javascript:DeleteAskToConfirm(@item.Id)" title="Supprimer">
                <i class="fa fa-trash-o"></i>
            </a>
        </td>
    </tr>
}
控制器中的

功能:

public JsonResult result(string examenId)
{
    List<CentreExamen> r= centreExamenService.GetAll().ToList();
    return Json(r, JsonRequestBehavior.AllowGet);
}

我正在使用firebug调试javascript函数我收到此错误:

  

Une exceptiondepremièremexde type'System.InvalidOperationException''s'product dans System.Web.Extensions.dll   Une例外depremièreme机构类型'System.InvalidOperationException's'est produite dans System.Web.Mvc.dll   Une exceptiondepremièremexde type'System.InvalidOperationException''s'product dans System.Web.Mvc.dll

这是我的javascript函数:

  function EditExamen(id) {

        $.getJSON("/Parametrage/result", {examenId:id}, function (data) {


            $.each(data, function (i, item) {
                 $('#CentresM').val(item.id); => this is the list what i want to fill and it is in modal pop up 

            });
        });

        $('#editExamen').modal(); 

    }
</script>

这是我的弹出窗口

<div id="editExamen" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Modifier un Examen</h4>
            </div>
            <div class="modal-body">
                <form name="form-editExamen" id="form-editExamen" action="ModifierExamen" method="post" class="form-horizontal">
                    <div class="form-group">
                        @Html.Hidden("examenIdM")
                        <div class="col-md-3">
                            <label>Intitulé :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.TextBoxFor(model => model.Item2.Intitule, new { id = "IntituleM", @class = "form-control" })
                        </div>
                        <div class="col-md-3">
                            <label>Lieu :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.DropDownList("DirectionsM", null, "Sélectionnez", new { @class = "form-control" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            <label>Centre :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.ListBox("CentresM", (MultiSelectList)ViewBag.Centres)  => this is my listbox

                        </div>
                        <div class="col-md-3">
                            <label>Note éliminatoire :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.TextBoxFor(model => model.Item2.NoteEliminatoire, new { id = "NoteEliminatoireM", @class = "form-control" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            <label>Moyenne de passage :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.TextBoxFor(model => model.Item2.MoyennePassage, new { id = "MoyennePassageM", @class = "form-control" })
                        </div>
                        <div class="col-md-3">
                            <label for="annee"> Année :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.DropDownList("AnneeM", null, new { @class = "form-control" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            <label for="grade"> Grade :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.DropDownList("GradeM", null, "Sélectionnez", new { @class = "form-control" })
                        </div>
                        <div class="col-md-3">
                            <label for="grade"> Date limite de réception des candidatures :</label>
                        </div>
                        <div class="col-md-3">
                            @Html.TextBoxFor(model => model.Item2.Datelimite, DateTime.Now.ToString("d"), new { id = "DatelimiteM", @class = "form-control datepicker" })
                        </div>

                    </div>
                    <div class="modal-footer">
                        <input type="submit" value="Modifier" class="btn btn-custom btn-lg" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
                    </div>
                </form>

            </div>

        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- **** deleteCritere **** -->

1 个答案:

答案 0 :(得分:0)

你的行为是向客户抛出异常。

public JsonResult result(string examenId)
{
   try{
    List<CentreExamen> r= centreExamenService.GetAll().ToList();
    return Json(r, JsonRequestBehavior.AllowGet);
   }
   catch(Exception e){
     return Json("exception occured while processing your request");
   }
}

现在处于调试模式,请检查您的centreExamenService.GetAll()方法。它抛出异常,或在这里发布方法。