如何在视图中拆分数据库中的值?

时间:2016-04-17 18:41:21

标签: asp.net asp.net-mvc asp.net-mvc-4 razor

我有一张名为book的表:

 public partial class Book
{
    public Book()

    [Key]
    public int Book_id { get; set; }

    [Required]
    [Display(Name = "Title Name")]
    [StringLength(70,MinimumLength =3)]
    public string Book_name { get; set; }

    [Display(Name = "Edition")]
    [Range(1, 20)]
    public int Edition { get; set; }    


    [StringLength(50)]
    [Display(Name = "Author Name")]
    public string Author_name { get; set; }



}

在此表中有一个名为Author_Name的属性。

Author_Name通常我在其中保存如下值:

Name1,Name2,Name3

并且在我看来我试图将其分割为每个人的特定链接,但我没有这样做..这是我的观看代码:

@model SmartBookLibrary.ViewModel.BooksDetailsVm

@{
    ViewBag.Title = Model.Book.Book_name;

    var s = Model.Book.Author_name.ToString();
    string[] Auth= s.Split(',');
}
<div class="container">

    <div class="row">
                    <p itemprop="author"><i class="glyphicon glyphicon-pencil"></i> <b>Author Name :</b>
@foreach(var val in Auth){
<a style="color:#777777" href="~/Author/@Model.Book.Author_name">@Auth.ToString();</a>
}

</p><hr>         </div>                                                                              </div>

因此,当我检查结果时,它没有打印任何内容或显示任何错误..如何解决这个问题并且谢谢..

1 个答案:

答案 0 :(得分:2)

尝试这种方式:

@{
   //var s = Model.Book.Author_name.ToString(); 
    var s = "Nazmul,khadiza,nowrin";
    string[] Auth = s.Split(',');
}
<div class="container">

    <div class="row">
        <p itemprop="author">
            <i class="glyphicon glyphicon-pencil"></i> <b>Author Name :</b>

        @for (int i = 0; i < Auth.Length; i++)
        {
            <a style="color:#777777">@Auth[i];</a>
        }

        </p><hr>
    </div>
</div>

或者如果在数组上调用.ToString()将返回System.String[],那么如果要显示数组中的每个值,请尝试以下操作:

 @foreach (string val in Auth)
            {
                <a style="color:#777777">@val;</a>
            }

输出: enter image description here