用逗号分隔的字符串数组

时间:2012-07-02 08:28:04

标签: asp.net-mvc-3

我有一个字符串数组,我希望它以逗号分隔的视图中返回。

@Html.DisplayFor(m => name.studentName) <span>, </span>}

我正在使用这种方式,但最后一个字符串也将以逗号结束。想知道如何避免这种情况?

3 个答案:

答案 0 :(得分:2)

我假设您的模型上有一组学生,每个学生都拥有您要显示的studentName属性:

public IEnumerable<Student> Students { get; set; }

在您的视图中,您将循环浏览此集合并单独显示每个学生姓名。

现在,您可以执行以下操作,而不是循环:

@Html.Raw(
    string.Join(
        "<span>,<span>",
        Model.Students.Select(x => Html.Encode(x.studentName))
    )
)

甚至更好,将此逻辑外部化为可重用的自定义HTML帮助程序:

public static class HtmlExtensions
{
    public static IHtmlString FormatStudentNames(this HtmlHelper htmlHelper, IEnumerable<Student> students)
    {
        return new HtmlString(
            string.Join(
                "<span>,<span>",
                students.Select(x => Html.Encode(x.studentName))
            )
        );
    }
}

然后在你的视图中只需调用这个帮助器:

@Html.FormatStudentNames(Model.Students)

你不再需要写任何foreach或你正在编写的任何循环。

答案 1 :(得分:1)

尝试

@string.Join(",", name.studentName);

看看string.Join on MSDN

答案 2 :(得分:0)

   $(".category").change(function () {
        var value = $(this).val();
        loadSubCategory(value)
    });

    function loadSubCategory(parentID) {

        var $el = $("#SubCats");
        $el.prop("disabled", true);
        $el.empty();
        $.ajax({
            cache: false,
            url: "/Category/loadSubCategory?id=" + parentID,
            success: function (data) {
                if (data != '' && data != null) {
                    if (data != 'error') {
                        var sch = JSON.parse(data);
                        if (sch.length > 0) {
                            $el.prop("disabled", false);
                            for (i = 0; i < sch.length; i++) {
                                $el.append($("<option></option>")
                                    .attr("value", sch[i].ID).text(sch[i].Description));
                            }
                        }
                    }
                }
            }
        });
    }

public ActionResult loadSubCategory(string id)
        {

            string list = "";
            try
            {

                list = Newtonsoft.Json.JsonConvert.SerializeObject(menu.SubCategory(id));
            }
            catch (Exception ex)
            {
            }
            return Content(list);

        }
  public List<CategoryModel> SubCategory(string parentID){

     List<CategoryModel> listCategory= new List<CategoryModel>();
      string[] yourValues = parentID.Split(',');
                    foreach (var item in yourValues)
                    {
                        var Category = UowObj.CategoryRepository.Get(filter: c => c.ParentId.ToString() == item && c.IsActive == true).ToList();
                        if (Category != null)
                        {
                            var category= new CategoryModel();
                            foreach (var itemq in Category)
                            {

                                category.ID = itemq.ID;
                                category.Description = itemq.Description;
                            }
                            listCategory.Add(merchant);
                        }

    }
相关问题