1

I had decided that there are just too many things that need to be different on the View Presentation Layer compared to what is actually in the database table.

I have a class of properties in C# that represents the model of my database HOWEVER I was reading about ViewModels and I can see how they can do the nice things of not displaying containing an unnecessary amount of data that you do not wish to display on your view, and it can also be an important aggregrate of combining multiple models in which you have much more available to you for trickling down to the View.

I simply tried to add a class with a property

  public class ExtendedFile
  {
     public string FileSize { get; set; }
  }

Then I figured I could simple add this as another property to my other model class , so I added it

public ExtendedFile ExtendedFile { get; set; }

Then it seems that I can in my controller simply hydrate this

 file.ExtendedFile.FileSize = ConvertFileSize(file.size);

So then my View now has

<td>@item.ExtendedFile.FileSize</td>

Well that did not work out .. A controller method had code in it in which a linq query that joins 2 tables freaked out. The error message is:

Invalid column name 'ExtendedFile_FileSize'.

The code that causes the error is:

var query = (
    from qtips in dbProd.tblTips 
    where qtips.id == 30 
    join files in dbProd.tblFiles on qtips.id.ToString() 
    equals files.@group select new { qtips, files }).ToList();
  • Show your controller code that is generating the error –  Aug 24 '15 at 03:14
  • var query = (from qtips in dbProd.tblTips where qtips.id == 30 join files in dbProd.tblFiles on qtips.id.ToString() equals files.@group select new { qtips, files }).ToList(); –  Aug 24 '15 at 03:15
  • So it tries to join on the dbProd.tblFiles and that tblFiles table DOES NOT have the FileSize column. –  Aug 24 '15 at 03:17
  • I was reading / googling and perhaps it is ok to just add the attribute [NotMapped] ? on top of the property? –  Aug 24 '15 at 03:17
  • 1
    But your question talks about view models. If `ExtendedFile` is not part of your data model, it should never be in your data model, only in the corresponding view model. And why would you have a class that contains only one property anyway? –  Aug 24 '15 at 03:20
  • Stephen, not all of us are well versed in many of the things that you have learned. The database tables has things like guids that map to image names and then size is in bytes and thus I end up needing to in code run the size (which is bytes) into a method that returns a string like 4 MB or 378 KB , thus the database table is not able to be changed and I am new to using ViewModel and ignored it for years as I didn't have jobs in which I ever had the need to even think about that tasks. –  Aug 24 '15 at 03:29
  • Also no , the ExtendedFile will end up with 6-12 properties so I just started it as a POC to make sure it is going to do what I need it to. I'm not sure that I need a ViewModel in my situation . Hey I greatly appreciate you and others looking into my question/problem. I'm very grateful. Recommendations? –  Aug 24 '15 at 03:29
  • You have not given enough information to understand what your actually trying to do (and I'm guessing your not understanding what a view model is). If your database has a field (say `Size int`) and your wanting to display it in the view as xxMB, then you need a view model containing a property `string FileSize` and you map you data model to the view model (converting the `int Size` field to `string FileSize`) –  Aug 24 '15 at 03:35
  • I read a lot of articles on ViewModel like this one http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications and have many sample applications I know that I can take say a class of 30 properties and have say a viewmodel that only wished to display first and last name in view or partialview etc.. , or i can combine multiple models to have a combined amount of data. Since I have like 10 fields that I do wish to display most of those from tblFiles , and then I will have say 6 properties in which I wish to "extend" my data model for the View. –  Aug 24 '15 at 03:43
  • That article may use the term ViewModel, but its not - its just a class that contains instances of data models (and defeats the whole concept of what a view model is). Start by reading [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Aug 24 '15 at 03:46
  • Thanks Stephen, I will read that now, as I was watching a video and now I see what you mean, I have been led down the wrong path! ugh. Thanks again Stephen –  Aug 24 '15 at 03:55

3 Answers3

2

Add the attribute as [NotMapped] on the property which is not column of database

Ravi
  • 475
  • 4
  • 12
  • 1. I said in the comments that I was going to try that, and it worked "I was reading / googling and perhaps it is ok to just add the attribute [NotMapped] ? on top of the property?" , but I'm realizing this a bad way to go with the fact that my whole purpose for doing this is to have a ViewModel that I can use that is part of database records and some not a part of them , –  Aug 24 '15 at 04:13
1

Thanks to Stephen while I realize that I "can" do use this other model, I'm just going really against the proper patterns in which what a true ViewModel is for

Thus I already had this working 40 minutes ago by simply adding the [NotMapped]

[NotMapped]
public ExtendedFile ExtendedFile { get; set; }

It exposes a bigger issue of the fact that I was not understanding the ViewModel.

So while that "works" I am going to instead have a ViewModel with

  1. The properties I want to display from the tblFiles class
  2. The properties from this this ExtendedFile class such as

    public class ExtendedFile
    {
        public string FileSize { get; set; }
        //more properties 
    }
    
0

This means that it can not find a field of the database that defined in the model.

For example, my problem was that I had written the "Connection string" with the test database wrong.

Rob
  • 26,989
  • 16
  • 82
  • 98
MohammadSoori
  • 2,120
  • 1
  • 15
  • 17