如何使用ViewData更新列表框的值

时间:2012-04-16 00:44:30

标签: c# javascript jquery asp.net-mvc listbox

我有一个用于生成列表的ViewData,在选择项目时,项目信息将显示在文本框中以供查看和编辑。单击保存按钮后,它会保存,但列表框没有更新,如何更新此列表框,因为我正在使用viewdata?

   @Html.ListBox("ListBoxName", new SelectList((IEnumerable<Epic>)ViewData["selectedestimate"], "Id", "Name", "EstimatedTime"), new {@class = "ListBoxClass", @style = "height: 325px;"})

   <button id="EpicSaveId" type="submit" class="btn" onclick="SaveEpicInfo((document.getElementById('EpicNameID').value), (document.getElementById('EpicTimeId').value), (document.getElementById('EpicDescriptionID').value))">

单击“保存”按钮时:(Javascript)

        function SaveEpicInfo(name, time, description) {
    $.get('@Url.Action("SaveEditEpic", "Estimate")', { id: eid, epicname: name, epictime: time, epicdescription: description }, function (result) {
        alert(result);

    });

}

控制器:

     [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult SaveEditEpic(int id, string epicname, double epictime, string epicdescription)
    {
        var epic = epicRepository.Select().SingleOrDefault(x => x.Id.Equals(id));
        if (epic != null)
        {
            epic.Name = epicname;
            epic.EstimatedTime = epictime;
            epic.EpicDescription = epicdescription;
            //ViewData["selectedestimate"] = epicRepository.Select().Where(x => x.EstimateId.Equals(ActiveEstimateId)).ToList();
            return Json("Epic Saved", JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json("Error");
        }
    }

1 个答案:

答案 0 :(得分:1)

您将不得不遍历这些选择数组并查找selected = true的选项,然后存储该值。