I have a table with various rows and each row corresponds to an ID. For every row, I have EDIT option which when selected should enable the user to look at the default values corresponding to that ID in the 'Edit.chtml' fields and there by update it by erasing the default values and typing in the new entry and saving it.
我有一个包含各种行的表,每行对应一个ID。对于每一行,我都有EDIT选项,选中此选项后,用户可以在“Edit.chtml”字段中查看与该ID对应的默认值,然后通过删除默认值并输入新条目来更新它。保存它。
In 'Edit.chtml', I have a field for "MANAGERS" that I need to populate with a dropdownlist of names with default/selected value displayed corresponding to the ID of the row where Edit operation is selected from the table. I am using viewBag but my problem is the dropdownlist is always displaying the first item in the list and not the selected value. Please see the code below.
在'Edit.chtml'中,我有一个“MANAGERS”字段,我需要填充一个名称下拉列表,其中显示的默认值/选定值对应于从表中选择编辑操作的行的ID。我正在使用viewBag但我的问题是下拉列表始终显示列表中的第一项而不是选定的值。请参阅下面的代码。
Controller:
//
// GET: /ManagersNAMES/Edit/5
public ActionResult Edit(int id)
{
using (var db = new InpEntities())
{
var list_managers = (from x in db.TABLE_MANAGERS
select new TABLE_MANAGERSDTO
{
ID = x.ID,
NAME = x.NAME,
}).OrderBy(w => w.NAME).ToList();
ViewBag.managers = new SelectList(list_managers, "ID", "NAME", id);
return View();
}
}
public class TABLE_MANAGERSDTO
{
public string NAME { get; set; }
public int ID { get; set; }
}
//
// POST: /ManagersNAMES/Edit/5
[HttpPost]
public ActionResult Edit(int id, TABLE_INSTITUTES dp)
{
try
{
using (var db = new InpEntities())
{
TABLE_INSTITUTES tmp = (TABLE_INSTITUTES)db.TABLE_INSTITUTES.Where(f => f.ID == id).First();
tmp.MANAGER = dp.MANAGER;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
return View();
}
}
View: 'Edit.chtml'
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label" style="font-weight:bold">MANAGER</div>
<div class="editor-field">
@Html.DropDownList("dp.MANAGER", (SelectList)ViewBag.managers)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
2 个解决方案
#1
2
You need to set the selected id within the controller like this:
您需要在控制器中设置所选的ID,如下所示:
var list_managers = list_managers.Select(x => new
{
Value = x.ID.ToString(),
Text = x.Name
})
.ToList();
ViewBag.managers = new SelectList(list_managers, "ID", "NAME", id);
And change your view to:
并将您的观点更改为:
@Html.DropDownList("dpMANAGER", (SelectList)ViewBag.managers)
#2
2
Just wrote @Html.DropDownList("dp.MANAGER", ViewBag.managers)
without selectlist, becouse its already in type of SelectList.
刚写了没有selectlist的@ Html.DropDownList(“dp.MANAGER”,ViewBag.managers),因为它已经是SelectList类型了。
And write ViewBag.managers = new SelectList(list_managers, "ID", "NAME");
like this
并编写ViewBag.managers = new SelectList(list_managers,“ID”,“NAME”);喜欢这个
#1
2
You need to set the selected id within the controller like this:
您需要在控制器中设置所选的ID,如下所示:
var list_managers = list_managers.Select(x => new
{
Value = x.ID.ToString(),
Text = x.Name
})
.ToList();
ViewBag.managers = new SelectList(list_managers, "ID", "NAME", id);
And change your view to:
并将您的观点更改为:
@Html.DropDownList("dpMANAGER", (SelectList)ViewBag.managers)
#2
2
Just wrote @Html.DropDownList("dp.MANAGER", ViewBag.managers)
without selectlist, becouse its already in type of SelectList.
刚写了没有selectlist的@ Html.DropDownList(“dp.MANAGER”,ViewBag.managers),因为它已经是SelectList类型了。
And write ViewBag.managers = new SelectList(list_managers, "ID", "NAME");
like this
并编写ViewBag.managers = new SelectList(list_managers,“ID”,“NAME”);喜欢这个