Unable to Use an HtmlHelper in Razor syntax in MVC4 Intranet App using Entity Framework

user3041439

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.

DJ.

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; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

From Dev

MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

From Dev

MVC Entity Framework Razor Issue

From Dev

Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

From Dev

Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

From Dev

MVC4: mimicking built-in HtmlHelper extensions, using lambdas

From Dev

PieChart in mvc4 from Entity Framework

From Dev

Dropdownlist and MVC4 Entity Framework

From Dev

Using Access Databases with razor mvc4

From Dev

Single or multiple DbContext File in mvc4 using Entity framework 6

From Dev

How to retrieve image from database without using Entity Framework in ASP.NET MVC4

From Dev

upload image in server folder and save path in Sqlserver using entity framework mvc4

From Dev

upload image in server folder and save path in Sqlserver using entity framework mvc4

From Dev

How to retrieve image from database without using Entity Framework in ASP.NET MVC4

From Dev

How to perform edit function in MVC4 without using entity framework?

From Dev

Crystalreportviewer not rendering (asp.net mvc4 razor syntax)

From Dev

Unable to use my HtmlHelper in views

From Dev

Log4net with Entity Framework 5 and MVC4

From Dev

Unable to use enum field in query in Entity Framework

From Dev

Unable to use enum field in query in Entity Framework

From Dev

Populate DropDownList using MVC 4 & Entity Framework

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

Will Orchard CMS support MVC4 with Entity Framework

From Dev

MVC4 Entity Framework Controller Database Issue

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

MVC4 Entity Framework 6 Model State Validation Issues

From Dev

REGEX to format decimals using superscript in MVC4 Razor / Javascript

From Dev

Unable to delete using dbSet - Entity Framework

From Dev

MVC4 ActionLink syntax for use with hidden text box values

Related Related

  1. 1

    How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?

  2. 2

    MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

  3. 3

    MVC Entity Framework Razor Issue

  4. 4

    Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

  5. 5

    Entity Framework profiler - ASP.NET MVC4 with EF 6 -Unable to determine the provider name

  6. 6

    MVC4: mimicking built-in HtmlHelper extensions, using lambdas

  7. 7

    PieChart in mvc4 from Entity Framework

  8. 8

    Dropdownlist and MVC4 Entity Framework

  9. 9

    Using Access Databases with razor mvc4

  10. 10

    Single or multiple DbContext File in mvc4 using Entity framework 6

  11. 11

    How to retrieve image from database without using Entity Framework in ASP.NET MVC4

  12. 12

    upload image in server folder and save path in Sqlserver using entity framework mvc4

  13. 13

    upload image in server folder and save path in Sqlserver using entity framework mvc4

  14. 14

    How to retrieve image from database without using Entity Framework in ASP.NET MVC4

  15. 15

    How to perform edit function in MVC4 without using entity framework?

  16. 16

    Crystalreportviewer not rendering (asp.net mvc4 razor syntax)

  17. 17

    Unable to use my HtmlHelper in views

  18. 18

    Log4net with Entity Framework 5 and MVC4

  19. 19

    Unable to use enum field in query in Entity Framework

  20. 20

    Unable to use enum field in query in Entity Framework

  21. 21

    Populate DropDownList using MVC 4 & Entity Framework

  22. 22

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  23. 23

    Will Orchard CMS support MVC4 with Entity Framework

  24. 24

    MVC4 Entity Framework Controller Database Issue

  25. 25

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  26. 26

    MVC4 Entity Framework 6 Model State Validation Issues

  27. 27

    REGEX to format decimals using superscript in MVC4 Razor / Javascript

  28. 28

    Unable to delete using dbSet - Entity Framework

  29. 29

    MVC4 ActionLink syntax for use with hidden text box values

HotTag

Archive