1

I am using C# MVC4, Razor syntax and Entity Framework for a simple intranet. I used EF to create my model from an existing database table called PrinterMapping. After creating it looks like this:

public partial class PrinterMapping
{
    public string MTPrinterID { get; set; }
    public string NTPrinterID { get; set; }
    public string Active { get; set; }
}

I extended the partial class so that it has a Property that is not in the database table but I want it there anyway.

public partial class PrinterMapping
{
    public string ExceptionMessage { get; set; }
}

In my HomeController's Create action, I set the ExceptionMessage Property based on catching any Exception messages returned from saving to the database. I want to display this Property on my Index.chtml view.

I am having trouble properly understanding strongly typed Html helpers that use Link. So I have:

@Html.DisplayTextFor(model => model.ExceptionMessage)

I get the following error while trying to run the app:

Compiler Error Message:

CS1061:
'System.Collections.Generic.IEnumerable<AccessPrinterMapping.PrinterMapping>'
does not contain a definition for 'ExceptionMessage' and no extension method 
'ExceptionMessage' accepting a first argument of type 
'System.Collections.Generic.IEnumerable<AccessPrinterMapping.PrinterMapping>'
could be found (are you missing a using directive or an assembly reference?)

Eh? EF earlier on created the following code for me and there are no problems with the following code:

<th align="left">
    @Html.DisplayNameFor(model => model.MTPrinterID)
</th>
<th align="left">
    @Html.DisplayNameFor(model => model.NTPrinterID)
</th>
<th align="left">
    @Html.DisplayNameFor(model => model.Active)
</th>

The only difference here is that MTPrinter, NTPrinter and Active Properties were created by EF and I created the ExceptionMessage Property myself.

Will some kind soul please explain a little bit of what is going on? Many thanks.

5
  • asp.net/mvc check and msdn.com - EF pattern Commented Nov 29, 2013 at 14:09
  • Is this in your index view? It sounds like you've placed the @Html.DisplayTextFor(model => model.ExceptionMessage) outside the loop. Commented Nov 29, 2013 at 14:14
  • I have struggled with the same issue before. I could handled it with [Bind(Exclude="ExceptionMessage")], but i prefer to keep the MVC convention for this sort of situation and create a ViewModel. Commented Nov 29, 2013 at 14:16
  • It is in my index view. Not sure which loop you are mention. Thanks. Commented Nov 29, 2013 at 14:17
  • Additionally please see the answer here: stackoverflow.com/questions/13535556/… Commented Nov 29, 2013 at 14:19

1 Answer 1

1

What is happening is that you have extended the PrinterMapping class definition and add a new property which is not defined either in the EF Model and not column exist in the database.

When you return the list of PrinterMapping the view try to fetch all the data from the database, but it fails beacuse it's unable to map the ExceptionMessage property.

You can use the Bind attribute (see Doc here), to either exclude or include your property in the object binding process:

public partial class PrinterMapping
{
    [Bind(Exclude="ExceptionMessage")]
    public string ExceptionMessage { get; set; }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I put the Bind attribute on top the property, but compiler says this is valid only for a class. So I put it on top of the partial class declaration. The problem does not go away. So I am no researching ViewModel(s) as per an earlier suggestion.
I'd go for the ViewModel as its a cleaner solution, there are good benefits (separate validation from UI and Business, separation of concerns between Model definition and UI, etc)
Thank you very much :) I now think extending the EF generated partial class was not the way to go.
OK. So in my Create controller, I now have ViewData["ExceptionMessage"] = //some message to output. What Html Helper do I use to show its value in the view? Thanks again.
I think I have it now. @Html.Label(ViewData["ExceptionMessage"] as String)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.