如何使用ViewBag将数据发送到视图后删除双引号?

时间:2014-11-27 14:18:13

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

我在编辑页面中有多对多关系如何显示在下拉列表中选择的所选项目

这是我的代码:

        // 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的新手

2 个答案:

答案 0 :(得分:2)

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>

答案 1 :(得分:2)

我同意@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>