C# 如何更新列表框';使用ViewData设置值
我有一个ViewData,用于在选择一个项目时生成一个列表,该项目信息将显示在文本框中以供查看和编辑。单击“保存”按钮后,它会保存,但列表框不会更新,既然我使用的是viewdata,如何更新此列表框C# 如何更新列表框';使用ViewData设置值,c#,javascript,jquery,asp.net-mvc,listbox,C#,Javascript,Jquery,Asp.net Mvc,Listbox,我有一个ViewData,用于在选择一个项目时生成一个列表,该项目信息将显示在文本框中以供查看和编辑。单击“保存”按钮后,它会保存,但列表框不会更新,既然我使用的是viewdata,如何更新此列表框 @Html.ListBox("ListBoxName", new SelectList((IEnumerable<Epic>)ViewData["selectedestimate"], "Id", "Name", "EstimatedTime"), new {@class = "L
@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))">
控制器:
[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");
}
}
您必须遍历这些select数组,查找select=true的选项,然后存储该值。Right..但是我如何访问该值呢。如何调用select数组。在另一个javascript中,我使用了类似$(“.ListBoxClass”)。单击(函数(事件){var selectedid=$(this.find(“option:selected”).val();这只返回一个值。在selectlist上设置一个id。这将与DOM上的
元素关联。按id选择
元素(或jQuery equiv),然后使用
元素上的.options[]
调用获取其选项。
[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");
}
}