I am getting compile errors when attempting to implement the message class from Levelnis's Blog.
The class in question is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
namespace Notifier.Helpers.MessageNotifier
{
public class Message
{
public MessageSeverity Severity { get; set; }
public string Text { get; set; }
public string Generate()
{
var isDismissable = Severity != MessageSeverity.Danger;
if (Severity == MessageSeverity.None) Severity = MessageSeverity.Info;
var sb = new StringBuilder();
var divTag = new TagBuilder("div");
divTag.AddCssClass("alert fade in");
divTag.AddCssClass("alert-" + Severity.ToString().ToLower());
var spanTag = new TagBuilder("span");
spanTag.MergeAttribute("id", "MessageContent");
if (isDismissable)
{
divTag.AddCssClass("alert-dismissable");
}
sb.Append(divTag.ToString(TagRenderMode.StartTag));
if (isDismissable)
{
var buttonTag = new TagBuilder("button");
buttonTag.MergeAttribute("class", "close");
buttonTag.MergeAttribute("data-dismiss", "alert");
buttonTag.MergeAttribute("aria-hidden", "true");
buttonTag.InnerHtml = "×";
sb.Append(buttonTag.ToString(TagRenderMode.Normal));
}
sb.Append(spanTag.ToString(TagRenderMode.StartTag));
sb.Append(Text);
sb.Append(spanTag.ToString(TagRenderMode.EndTag));
sb.Append(divTag.ToString(TagRenderMode.EndTag));
return sb.ToString();
}
}
}
I am getting two errors - the first relates to ".ToString" on the line:
sb.Append(divTag.ToString(TagRenderMode.StartTag));
and it says:
No overload for method 'ToString' takes 1 arguments
The second error relates to the line:
buttonTag.InnerHtml = "×";
and it says:
Property or indexer 'TagBuilder.InnerHtml' cannot be assigned to -- it is read only
I have had a look at the documentation and other posts but so far I have not found anything that can help me modify this to compile correctly...
Are these methods implemented differently in Asp.net-core and if not can someone suggest another way of doing these lines?
This was all to do with implementing a way of placing messages on the subsequent page - initially using Levilnis's Blog.
Not able to implement this without issues I stepped back and looked for another way of doing this. I used an approach presented by Mister James and in particular using a baseController to hold all my possible messages. I simplified it a bit as I didn't need to run multiple messages - just one per page.
There was a wrinkle in that you cannot, in MVC6, send complex objects using Tempdata. This was solved using @Hem's answer in this Stackoverflow question where he suggests using an extension that converts the complex object into a json string. For this to work I did have t oadd the using "using Newtonsoft.Json;".
This now works like a charm.