Home > Software design >  How to set additional values on model for Enum values?
How to set additional values on model for Enum values?

Time:12-31

I'm brand new to the ASP.NET MVC world.

I'm working on an MVC widget in Sitefinity. It is to create an alert on the page. Below is how the View looks. It's all built with Bootstrap 5 on the front end.

Based on the option selected from the dropdown, alert- will be appended with, for example, danger and the class alert-danger is what's passed to the front end. It all works great, but in the Enum, I'd like to display the values to start with a capital letter.

If I do that, then the output class becomes:

alert-Danger, alert-Success, etc.

This video provides a display of what my result is.

enter image description here

Model

namespace SitefinityWebApp.Mvc.Models
{
    public enum AlertStatus
    {
        primary, 
        secondary, 
        success, 
        danger, 
        warning, 
        info
    }
    public class AlertModel
    {
        public string AlertText { get; set; }
        public AlertStatus Enum { get; set; }
    }
}

View

@model SitefinityWebApp.Mvc.Models.AlertModel

<div  role="alert">
    @Model.AlertText
    <button type="button"  data-bs-dismiss="alert" aria-label="Close"></button>
</div>

Controller

using SitefinityWebApp.Mvc.Models;
using System.ComponentModel;
using System.Web.Mvc;
using Telerik.Sitefinity.Mvc;

namespace SitefinityWebApp.Mvc.Controllers
{
    [ControllerToolboxItem(Name = "Alert", Title = "Alert", SectionName = "COS Widgets")]
    public class AlertController : Controller
    {
        // GET: Alert
        public ActionResult Index()
        {
            var model = new AlertModel();
            model.AlertText = this.AlertText;
            model.Enum = this.Enum;
            return View(model);
        }
        
        protected override void HandleUnknownAction(string actionName)
        {
            this.ActionInvoker.InvokeAction(this.ControllerContext, "Index");
        }

        [DisplayName("Alert Text")]
        public string AlertText { get; set; }

        [DisplayName("Alert Status")]
        public AlertStatus Enum { get; set; }
    }
}

CodePudding user response:

In the View, you can just use this:

[email protected]().ToLower() to make it lowercase.

  • Related