Topic how to find out if the topic exists

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 năm cách đây
Hi

I am trying to workout how I can find out if a topic exists or not in a view, so for example ho can I do

If @Html.Action("TopicBlock", "Topic", new { systemName = "TopicName" }) exists then
Do this
else
Do This


So what would this @Html.Action("TopicBlock", "Topic", new { systemName = "TopicName" }) line of code need to be

As you can tell I am new to MVC

Thanks
Spire
11 năm cách đây
The TopicBlock action returns an empty string ("") if the topic with the systemName doesn't exist; therefore, you can check the length of the content returned by Html.Action() to see if a topic exists.
@{
    var topic = Html.Action("TopicBlock", "Topic", new { systemName = "TopicName" });
}

@if (topic.ToString().Length > 0)
{
    <div>Topic exists</div>
    @topic
}
else
{
    <div>Topic doesn't exist</div>
}
.
11 năm cách đây
Hi

Thanks for you reply, however I don't know if I am missing somthing, but I was unable to make your code work, I kept getting an exception telling me that Html.Action("TopicBlock", "Topic", new { SystemName = Model.SeName.Replace("-", "") })) is not a string.

Anyway after further research I discoverd that Html.Action("TopicBlock", "Topic", new { SystemName = Model.SeName.Replace("-", "") })) is a  MvcHtmlString and has the IsNullOrEmpty method, so my final solution is below.

@if (MvcHtmlString.IsNullOrEmpty(Html.Action("TopicBlock", "Topic", new { SystemName = Model.SeName.Replace("-", "") })))
{
<p> Do Something</p>
}
else
{
<p> Do Nothing</p>
}

The above solution is so that I can grab the product name and if there is a topic with the same product name with no spaces in the topic name then I display the topic.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.