mvc cshtml 字符串操作
@using System.Text; @{ int js = 1; string str=""; StringBuilder sb = new StringBuilder(); }
@foreach (var item in ViewBag.Doctypes) { if (item.DocTypeNo.Length == 5) { sb.AppendFormat("<optgroup label='{0}'>", item.DocTypeName); foreach (var item1 in ViewBag.Doctypes) { if (item1.DocTypeNo.Length < 6)//字符串小于6跳过 { continue; } if (item.DocTypeNo == item1.DocTypeNo.Substring(0, 5)) //判断前5个是否相同 { if (item1.DocTypeName == ViewBag.Current) { sb.AppendFormat("<option value='{0}' selected>{1}</option>", @item1.DocTypeName, @item1.DocTypeName); //<option value="@item1.DocTypeName" selected>@item1.DocTypeName</option> continue; } sb.AppendFormat("<option value='{0}'>{1}</option>", @item1.DocTypeName, @item1.DocTypeName); } } sb.Append("</optgroup>" ); } } @Html.Raw(sb.ToString())