如何在发送数据以使用 ViewBag 查看后删除双引号
本文关键字:删除 ViewBag 数据 | 更新日期: 2023-09-27 18:32:25
我在编辑页面中有多对多关系,如何在下拉列表中显示选定的项目 多选
这是我的代码:
// GET: Project/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var edata = db.Projects.Find(id);
//check if data
if (edata == null)
return HttpNotFound();
//IEnumerable<SelectListItem> items = db.Provinces.Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.Name });
//ViewBag.ProvinceId = items;
//---Get all provice-----
var Prov = from c in db.Provinces select c;
//------get province ids---------------
var prov_id = from o in db.ProRel where o.ProjectId == id select o.ProvinceIds;
List<int> mid_list = new List<int>();
foreach (var p_ids in prov_id)
{
mid_list.Add(p_ids);
}
var option_vals = "";
var selected = string.Empty;
foreach (var p_itmes in Prov)
{
if (mid_list.Contains(p_itmes.ID))
{
selected = "selected='selected'";
}
else
{
selected = "";
}
option_vals += "<option "+selected+" value="+p_itmes.ID+">"+p_itmes.Name+"</option>";
}
string test = option_vals.ToString();
string test2 = test.Replace("'"", "");
ViewBag.options = test2;
return View(edata);
}
在我的编辑视图中,我的代码:
<div class="form-group">
<label class="control-label col-md-2">Provinces</label>
<div class="col-md-10">
<select name="prov" id="prov">
@ViewBag.options
</select>
</div>
</div>
它在末尾用双引号显示选项值,并且不像 Html 选择选项那样呈现,如何删除我的选项应在其正常模式下显示的引号。
当我在浏览器中检查元素时,选项如下所示:
"<option value=1>Kabul</option><option selected='selected' value=2>Mazar</option><option selected='selected' value=3>Parwan</option><option value=4>Herat</option><option value=5>Badakhshan</option><option value=6>Takhar</option><option value=7>Smanagan</option><option selected='selected' value=8>Zabul</option>"
或者你建议我另一种方式,我是新手 ASP.Net
我同意@ChrFin答案,但我希望能解决您的问题的更多细节
// GET: Project/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var edata = db.Projects.Find(id);
//check if data
if (edata == null)
return HttpNotFound();
//IEnumerable<SelectListItem> items = db.Provinces.Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.Name });
//ViewBag.ProvinceId = items;
//---Get all provice-----
var Prov = from c in db.Provinces select c;
//------get province ids---------------
var prov_id = from o in db.ProRel where o.ProjectId == id select o.ProvinceIds;
List<int> mid_list = new List<int>();
foreach (var p_ids in prov_id)
{
mid_list.Add(p_ids);
}
var options = new List<SelectListItem>();
foreach (var p_itmes in Prov)
{
var item = new SelectListItem();
if (mid_list.Contains(p_itmes.ID))
item.Selected = true;
item.Value = p_itmes.ID.ToString();
item.Text = p_itmes.Name;
options.Add(item);
}
ViewBag.options = options;
return View(edata);
}
并在视图中添加以下额外属性:
<div class="form-group">
<label class="control-label col-md-2">Provinces</label>
<div class="col-md-10">
@Html.DropDownList("prov", (List<SelectListItem>)ViewBag.options, htmlAttributes: new { @class = "form-control", @multiple = "multiple" })
</div>
</div>
Marcos 已经通过他的评论给出了简短的答案,但总的来说,这不是您在 MVC 5 中创建select
标签的方式:
控制器:
public ActionResult Edit(int? id)
{
// other code
var options = new List<SelectListItem>();
foreach (var p_itmes in Prov)
{
var item = new SelectListItem();
if (mid_list.Contains(p_itmes.ID))
item.Selected = true;
item.Value = p_itmes.ID.ToString();
item.Text = p_itmes.Name;
options.Add(item);
}
ViewBag.options = options;
return View(edata);
}
视图:
<div class="form-group">
<label class="control-label col-md-2">Provinces</label>
<div class="col-md-10">
@Html.DropDownList("prov", (List<SelectListItem>)ViewBag.options,
htmlAttributes: new { multiple = "multiple" })
</div>
</div>