Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random ids for tables that is inside a foreach loop in a view with MVC

Im using MVC-Viewmodel, EF Model first on my project.

I have a foreach loop that generates 4 tables inside my view. I want that each table that generates gets an unique ID. I guess that I have to do this inside my ViewModel.

Any kind help is appreciated All I know is that I need to declare something like this inside my foreach loop:

<table id="RandomID_@("MyRandomProperty)">

and probably a property like this:

public random MyRandomProperty {get;set;}

Thanks in advance!

like image 208
Obsivus Avatar asked Dec 27 '22 03:12

Obsivus


2 Answers

You could use a Guid:

<table id="RandomID_@(Guid.NewGuid())">
like image 102
Darin Dimitrov Avatar answered Apr 06 '23 00:04

Darin Dimitrov


How about using a GUID as your ID property?

private Guid _tableId;
public Guid TableId = {
    get {
        if(_tableId == null)
            _tableId = Guid.NewGuid();

         return _tableId;
     }
 }

Or you could just call Guid.NewGuid() inline in your View? Dependes if you need to access the ID from else where in your code...

Hope this helps,

James

like image 45
james lewis Avatar answered Apr 06 '23 00:04

james lewis