Reputation: 79
My question would be complicated but I will try to ask clearly.
I want to keep tags in a data at SQL, as using the comma in a cell. Then, try to call two different way on my MVC 4 project. One way (basic one) is working. But another one is hard to run. I will give my codes one by one.
Controller:
public ActionResult Portfolio()
{
return View(db.Portfolios.ToList());
}
View:
@model IEnumerable<emirhanozkan.Models.Portfolio>
@{
foreach (var pot in Model)
{
<p>@pot.Tags.Split(',')[0]</p>
<p>@pot.Tags.Split(',')[1]</p>
<p>@pot.Tags.Split(',')[2]</p>
}
}
In same View one more foraech is running:
@foreach (var po in Model)
{
<li class="item @po.Tags.ToLower().Trim().Replace(",","")">
<img src="@po.Image" />
<a href="@po.Url" target="_blank" class="md-button md-primary">@po.Title</a>
</li>
}
So, What I want to do with Split method:
<p>AngularJS</p>
<p>MVC</p>
<p>C#</p>
<p>Wordpress</p>
<p>MVC</p>
I guess my @pot.Tags.Split(',')[0]
, @pot.Tags.Split(',')[1]
and @pot.Tags.Split(',')[2]
code is wrong to list them but my brain not working anymore than that one. Please help me get them like my dream. Also, if you now to get just one from repeat words like <p>MVC</p> <p>MVC</p>
, to just <p>MVC</p>
please add to new code.
Upvotes: 0
Views: 425
Reputation: 79
Thank you for your suggestions. But I found the answer to my question:
@{
foreach (var pot in Model)
{
string str = pot.Tags;
string[] strT = str.Split(',');
foreach (var poo in strT) {
<p>@poo</p>
}
}
}
Upvotes: 0
Reputation: 583
foreach (var pot in Model)
{
var tags = @pot.Tags.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
foreach(var tag in tags)
{
<p>@tag</p>
}
}
foreach (var pot in Model)
{
<li>
<img src="@po.Image" />
<a href="@po.Url" target="_blank" class="md-button md-primary">
@po.Title</a>
</li>
}
Upvotes: 0
Reputation: 312
sorry for errors, I write without compile:
List<Portfolio> Model = new List<Portfolio>();
StringBuilder finaltags= new StringBuilder();
foreach (var pot in Model)
{
finaltags.AppendLine("<p>" + @pot.Tags.Split(',') + "</p>");
}
@Html.Raw(finaltags.ToString());
Upvotes: 0
Reputation: 62498
You can just loop on the array returned by Split()
method and then render the tags:
foreach (var pot in Model)
{
var tags = @pot.Tags.Split(',');
foreach(var tag in tags)
{
<p>@tag</p>
}
}
Upvotes: 1