0

The following Model holds Registration details

public class Registration
{
    public int RegistrationID { get; set; }

    [Required(ErrorMessage = "Enter Name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Mobileno Required")]
    [Display(Name = "Mobile No")]
    [RegularExpression(@"^(\d{10})$", ErrorMessage = "Wrong Mobileno")]
    public string Mobileno { get; set; }

    [Required(ErrorMessage = "EmailID Required")]
    [Display(Name = "Email")]

    public string EmailID { get; set; }
}

The following holds assigned Records to users in the Registration Model above

public class AssignedRoles 
{
    [Key]
    public int AssignedRolesID { get; set; }
    public int? AssignToAdmin { get; set; }
    public int? CreatedBy { get; set; }
    public DateTime? CreatedOn { get; set; }
    public int RegistrationID { get; set; }
    public string Status { get; set; }
    public DateTime? ModifiedOn { get; set; }
    [DataType(DataType.Time)]
    public string TurnAroundTime { get; set; }
    public virtual Incidence Incidence { get; set; }
    public int RegID { get; set; }
}

Below is the view

<table id="example1" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>TSC NO</th>
            <th>Name</th>
            <th>Cell</th>
            <th>Request</th>
            <th>Request Type</th>
            <th>Incidence Type</th>
            <th>Duration</th>
            <th>Assigned To</th>
        </tr>
    </thead>
    @foreach (var item in Model)
    {

        <tr>
            <td>@item.Incidence.TSCNO</td>
            <td>@item.Incidence.Name</td>
            <td>@item.Incidence.CellPhone</td>
            <td>@item.Incidence.Request</td>
            <td>@item.Incidence.RequestType</td>
            <td>@item.Incidence.IncidenceType</td>
            <td>@item.TurnAroundTime</td>
            <td>Username goes here</td>
        </tr>
    }

</table>

Whenever an item is assigned to a user, the registrationID which serves as the UserID is pushed to the AssignToAdmin column in AssignedRoles.

However, from the view, I would like to display the Name of the Admin instead of the ID.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Nickson
  • 135
  • 12
  • I do not think I 100% understand what you saying in your question, can you elaborate. – Maytham Fahmi Apr 26 '20 at 15:30
  • I have a list of users and an Admin. This is a help desk ticketing system. Each ticket after being assigned is pushed to the assignedroles model. The column assigntoadmin holds the ID of the person who was assigned that ticket. From this model. I would like to display the name onto that view instead of the ID. Take not that the view calls the model Ienumerable – Nickson Apr 26 '20 at 16:30

1 Answers1

0

You should look into creating a view model for the information this specific page needs to represent.

See https://stackoverflow.com/a/11074506/13374279 for a much more verbose answer, but you have two models shown above; your "Registration" model looks like an input model where you're letting users input properties about a user that you're storing out somewhere, and your "AssignedRoles" model looks like a domain model, where you're storing relationships "behind the scenes," probably in a database or other persistence layer.

However, when you want to represent that information in your view, you need slightly different information than you do when you're storing it, and this is where a view model would come in. The view model can combine information from several different domain models to provide the specific data elements required to render it within your view. Within your backend code, you might query both the domain model for your registered users and for your registrations, and combine them into a single view model that contains the name.

The following example is abbreviated for clarity to illustrate the concept:

    public class RegistrationDomainModel
    {
        public int RegistrationId { get; set; }

        public string PropertyString { get; set; }

        public int UserId { get; set; }
    }

    public class UserDomainModel
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
    }

    public class RegistrationViewModel
    {
        public string PropertyString { get; set; }

        public string UserName { get; set; } // we don't care about displaying the user's ID here, just the name
    }

    public class MyController : Controller
    {

        [HttpGet]
        public IActionResult Get(int registrationId)
        {
            var viewModel = new RegistrationViewModel();

            // We can go do whatever work is needed to collect and relate the information we need to display. This is a very dirty example.
            var myRegistrationDomainModel = _registrationDatabase.Get(registrationId);
            var myUserDomainModel = _userDatabase.Get(myRegistrationDomainModel.UserId);

            // The view model is generated with the information needed for display only.
            viewModel.PropertyString = myRegistrationDomainModel.PropertyString;
            viewModel.UserName = myUserDomainModel.UserName;

            return View(viewModel);
        }
    }
Adam
  • 3,339
  • 1
  • 10
  • 15