javascript - How to get Enum value in Razor - Stack Overflow

I used datatables and I want to get Enum value for a specified int variable.var client = $('td

I used datatables and I want to get Enum value for a specified int variable.

var client = $('td', row).eq(1).text().trim();

client is a number data type.

I tried this, but it is not working:

var [email protected](typeof(Client),client);

client does not exist in the current context

Thanks in advance!

I used datatables and I want to get Enum value for a specified int variable.

var client = $('td', row).eq(1).text().trim();

client is a number data type.

I tried this, but it is not working:

var [email protected](typeof(Client),client);

client does not exist in the current context

Thanks in advance!

Share Improve this question asked Apr 11, 2017 at 6:31 Mihai Alexandru-IonutMihai Alexandru-Ionut 48.5k14 gold badges105 silver badges132 bronze badges 16
  • You cannot mix javascript and c# code like that (one runs on the client and one runs on your server) – user3559349 Commented Apr 11, 2017 at 6:33
  • you can't mix js with razor. – mmushtaq Commented Apr 11, 2017 at 6:33
  • How can i resolve the problem ? – Mihai Alexandru-Ionut Commented Apr 11, 2017 at 6:34
  • You can only mix JS and C#/Razor in a server->client direction. That is, server-side values can be set once at the time the response is sent to the browser, before the browser sees it. – nnnnnn Commented Apr 11, 2017 at 6:35
  • 1 You could use ajax to post the value to the server and return the text value. Or you could pass a collection/dictionary or the enums int and name values to the view and convert it to a javascript array and search that array for the corresponding value. – user3559349 Commented Apr 11, 2017 at 6:38
 |  Show 11 more ments

1 Answer 1

Reset to default 5

@Enum.GetName(typeof(Client), client); is razor code which is parsed on the server before its sent to the view. client is a javascript variable which does not exist at that point (its not in scope).

On option would be to pass a collection of the enum names and values to the client, and assign it to a javascript array so it can be searched.

For example, create a class to represent the names and values

public class EnumValues
{
    public int ID { get; set; }
    public string Name { get; set; }
}

and in your controller

IEnumerable<EnumValues> clientValues = 
    from Client c in Enum.GetValues(typeof(Client))
    select new EnumValues
    {
        ID = (int)c,
        Name = c.ToString()
    };

and pass it to the view in a view model property, or using ViewBag

ViewBag.ClientValues = clientValues;

In the view, assign it to a javascript array, and create a function to return the name for a corresponding value

var clients = @Html.Raw(Json.Encode(ViewBag.ClientValues));

function getClientName(value)
{
    for(var x = 0; x < clients.length; x++) {
        if (clients[i].ID == value) {
            return clients[i].Name;
        }
    }
    return null;
}

and use it as

var client = $('td', row).eq(1).text().trim();
var clientName = getClientName(client);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744491218a4576856.html

相关推荐

  • javascript - How to get Enum value in Razor - Stack Overflow

    I used datatables and I want to get Enum value for a specified int variable.var client = $('td

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信