0

I just recently started creating a .net web app in mvc5. I've tried to search for the answer as I usually do rather than asking a question but Im not sure how to word this but here it goes:

I have a class:

public partial class employee
{
   public string FirstName { get; set; }
   public string MiddleName { get; set; }
   public string LastName { get; set; }
}

In which the class was created by using the Database First wizard with Entity Framework. (reasoning for the tag)

What I did was get the FistName, MiddleName, and LastName from the database. In which I successfully did with this ActionResult from my controller.

[ChildActionOnly]
 public ActionResult Nav()
 {
     employee user;
     using (DatabaseEntities context = new DatabaseEntities())
     {
         user = context.employees.FirstOrDefault(e => e.FirstName ="Bob");
         if (!System.IO.File.Exists(Server.MapPath(user.imagefile)))
         {
              user.imagefile = $"/Content/Images/user/{user.FirstName[0]}.png";
         }
     }
     return PartialView("_Nav", user);
 }

Now I'm wondering if its possible, how to create another item to the class without adding a column to the database. I want to add a FormattedName variable to the class that is got after the database call to get the model (user) to include the new variable (FormattedName) that gets its value from the existing variables in the class.

I understand the wording and terminology could be off but any help would be great and thx in advance.

Edit:

In the comment section, I was informed about attributes ex.[NotMapped] which was information I definitely needed to know. But for this particular question, it doesn't work. Everything was ok until I updated the model using entity framework, the attributes were gone and have to keep adding them every model update which is not ideal for me. I went ahead and edited my database to include a (FormattedName) column so the problem is fixed. But I still would love to know if this is doable.

Bigg_aye
  • 131
  • 2
  • 10
  • 4
    You can use the [NotMapped](https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/notmapped-attribute) attribute, but its better to create a view model representing what you want in the view. –  Nov 20 '18 at 00:19
  • In which class you wanted to add your new item? You can add it before `return PartialView("_Nav", user);` – Vijunav Vastivch Nov 20 '18 at 00:26
  • @StephenMuecke The [NotMapped] attribute worked perfectly. Thank you. But can you explain how and why is it best to create a view model representing the view for better understanding? Or would I have to post another question? – Bigg_aye Nov 20 '18 at 00:40
  • 1
    Start with [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). And if you editing data, it should be mandatory to use a view model (some of the reasons are explained in that link, but there are others such as [What does it mean for a property to be [Required] and nullable?](https://stackoverflow.com/questions/43688968/what-does-it-mean-for-a-property-to-be-required-and-nullable/43689575#43689575) –  Nov 20 '18 at 00:45
  • @StephenMuecke thx for the info. Appreciate your time. – Bigg_aye Nov 20 '18 at 00:51

0 Answers0