Hi guys, I'm trying to get my head around this strongly typed view concept but I keep getting an error hen I view the page. Basically, my application is called modelsTest and I have a CompanyInfo.cs class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace modelsTests.Models
{
    public class CompanyInfo
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

In the HomeController.cs the About ActionResult method creates a variable and initialize the property of the CompanyInfo class and when I return the View there are two parameters: the name of the view to return and the variable I used to initialize the properties:

public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";
            //ViewBag.CompanyName = "Company X";
            //ViewBag.CompanyDescription = "Company X does A, B, C and D.";

            var company = new CompanyInfo
            {
                Name = "Company X",
                Description = "Company X does A, B, C and D.",
            };
            return View("About", company);
        }

Now, in the About.cshtml I want to display the values of those properties using a strongly typed view, which, from what I understand, will allow me to reference the variables Name and Description directly. So at the top of the About.cshtml page I need to include, presumably, @model CompanyInfo but visual studio doesn't like that. Am I missing something really obvious?

Hi

When you say Visual Studio doesn't like it, what does it say?

I tried out your scenario and didn't have any problems. First I created your CompanyInfo class and then replaced the About controller action with yours. Then in the About View I added @model modelsTests.Models.CompanyInfo and proceeded to output the Name and Description properties which worked fine. My entire About view looks like:

@model modelsTests.Models.CompanyInfo
@{
    ViewBag.Title = "About";
}


<p>Use this area to provide additional information.</p>
<p>@Model.Name</p>
<p>@Model.Description</p>

Note, I had to provide the full namespace to the model, if you don't want to do that then you can either add a using statement above that line: @using modelsTests.Models or add the namespace to your web.config file in the Views folder (not the root folder).

HTH

Hi thanks. The first thing I thought about after posting this was the fact that I didn't fully qualified the namespace, so I changed it to @model modelsTests.Models.CompanyInfo;, but still no luck. When I said "Visual Studio doesn't like it", I should have been a bit more explicit, sorry for that, but basically the application builds OK even though modelsTests.Models.CompanyInfo; is underlined in VS and if I hover on it, a message appears and says:
"namespace modelsTests.Models; Only assignment, call, increment, decrement, await and new object expression can be used as a statement"
Then after the building, I press CTRL+F5 to view the page and when I click on the About link I get a compilation error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1003: Syntax error, '>' expected

Source Error:


Line 32:     
Line 33:     
Line 34:     public class _Page_Views_Home_About_cshtml : System.Web.Mvc.WebViewPage<modelsTests.Models.CompanyInfo;> {
Line 35:         
Line 36: #line hidden

And here is the compilation source:

Line 1:    #pragma checksum "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "9400F1A931EAA0E84BBB6D5E6808CFCBFC599BBC"
Line 2:    //------------------------------------------------------------------------------
Line 3:    // <auto-generated>
Line 4:    //     This code was generated by a tool.
Line 5:    //     Runtime Version:4.0.30319.18063
Line 6:    //
Line 7:    //     Changes to this file may cause incorrect behavior and will be lost if
Line 8:    //     the code is regenerated.
Line 9:    // </auto-generated>
Line 10:   //------------------------------------------------------------------------------
Line 11:   
Line 12:   namespace ASP {
Line 13:       using System;
Line 14:       using System.Collections.Generic;
Line 15:       using System.IO;
Line 16:       using System.Linq;
Line 17:       using System.Net;
Line 18:       using System.Web;
Line 19:       using System.Web.Helpers;
Line 20:       using System.Web.Security;
Line 21:       using System.Web.UI;
Line 22:       using System.Web.WebPages;
Line 23:       using System.Web.Mvc;
Line 24:       using System.Web.Mvc.Ajax;
Line 25:       using System.Web.Mvc.Html;
Line 26:       using System.Web.Optimization;
Line 27:       using System.Web.Routing;
Line 28:       using WebMatrix.Data;
Line 29:       using WebMatrix.WebData;
Line 30:       using Microsoft.Web.WebPages.OAuth;
Line 31:       using DotNetOpenAuth.AspNet;
Line 32:       
Line 33:       
Line 34:       public class _Page_Views_Home_About_cshtml : System.Web.Mvc.WebViewPage<modelsTests.Models.CompanyInfo;> {
Line 35:           
Line 36:   #line hidden
Line 37:           
Line 38:           public _Page_Views_Home_About_cshtml() {
Line 39:           }
Line 40:           
Line 41:           protected ASP.global_asax ApplicationInstance {
Line 42:               get {
Line 43:                   return ((ASP.global_asax)(Context.ApplicationInstance));
Line 44:               }
Line 45:           }
Line 46:           
Line 47:           public override void Execute() {
Line 48:               
Line 49:               #line 3 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 50:     
Line 51:       ViewBag.Title = "About";
Line 52:   
Line 53:               
Line 54:               #line default
Line 55:               #line hidden
Line 56:   BeginContext("~/Views/Home/About.cshtml", 77, 11, true);
Line 57:   
Line 58:   WriteLiteral("\r\n\r\n<hgroup");
Line 59:   
Line 60:   EndContext("~/Views/Home/About.cshtml", 77, 11, true);
Line 61:   
Line 62:   BeginContext("~/Views/Home/About.cshtml", 88, 14, true);
Line 63:   
Line 64:   WriteLiteral(" class=\"title\"");
Line 65:   
Line 66:   EndContext("~/Views/Home/About.cshtml", 88, 14, true);
Line 67:   
Line 68:   BeginContext("~/Views/Home/About.cshtml", 102, 11, true);
Line 69:   
Line 70:   WriteLiteral(">\r\n    <h1>");
Line 71:   
Line 72:   EndContext("~/Views/Home/About.cshtml", 102, 11, true);
Line 73:   
Line 74:   BeginContext("~/Views/Home/About.cshtml", 114, 13, false);
Line 75:   
Line 76:               
Line 77:               #line 8 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 78:      Write(ViewBag.Title);
Line 79:   
Line 80:               
Line 81:               #line default
Line 82:               #line hidden
Line 83:   EndContext("~/Views/Home/About.cshtml", 114, 13, false);
Line 84:   
Line 85:   BeginContext("~/Views/Home/About.cshtml", 127, 16, true);
Line 86:   
Line 87:   WriteLiteral(".</h1>\r\n    <h2>");
Line 88:   
Line 89:   EndContext("~/Views/Home/About.cshtml", 127, 16, true);
Line 90:   
Line 91:   BeginContext("~/Views/Home/About.cshtml", 144, 15, false);
Line 92:   
Line 93:               
Line 94:               #line 9 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 95:      Write(ViewBag.Message);
Line 96:   
Line 97:               
Line 98:               #line default
Line 99:               #line hidden
Line 100:  EndContext("~/Views/Home/About.cshtml", 144, 15, false);
Line 101:  
Line 102:  BeginContext("~/Views/Home/About.cshtml", 159, 40, true);
Line 103:  
Line 104:  WriteLiteral("</h2>\r\n</hgroup>\r\n\r\n<article>\r\n    <p>\r\n");
Line 105:  
Line 106:  EndContext("~/Views/Home/About.cshtml", 159, 40, true);
Line 107:  
Line 108:  BeginContext("~/Views/Home/About.cshtml", 199, 7, true);
Line 109:  
Line 110:  WriteLiteral("       ");
Line 111:  
Line 112:  EndContext("~/Views/Home/About.cshtml", 199, 7, true);
Line 113:  
Line 114:  BeginContext("~/Views/Home/About.cshtml", 207, 12, false);
Line 115:  
Line 116:              
Line 117:              #line 14 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 118:    Write(company.Name);
Line 119:  
Line 120:              
Line 121:              #line default
Line 122:              #line hidden
Line 123:  EndContext("~/Views/Home/About.cshtml", 207, 12, false);
Line 124:  
Line 125:  BeginContext("~/Views/Home/About.cshtml", 219, 10, true);
Line 126:  
Line 127:  WriteLiteral(";\r\n       ");
Line 128:  
Line 129:  EndContext("~/Views/Home/About.cshtml", 219, 10, true);
Line 130:  
Line 131:  BeginContext("~/Views/Home/About.cshtml", 255, 23, true);
Line 132:  
Line 133:  WriteLiteral("\r\n    </p>\r\n\r\n    <p>\r\n");
Line 134:  
Line 135:  EndContext("~/Views/Home/About.cshtml", 255, 23, true);
Line 136:  
Line 137:  BeginContext("~/Views/Home/About.cshtml", 278, 7, true);
Line 138:  
Line 139:  WriteLiteral("       ");
Line 140:  
Line 141:  EndContext("~/Views/Home/About.cshtml", 278, 7, true);
Line 142:  
Line 143:  BeginContext("~/Views/Home/About.cshtml", 286, 19, false);
Line 144:  
Line 145:              
Line 146:              #line 19 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 147:    Write(company.Description);
Line 148:  
Line 149:              
Line 150:              #line default
Line 151:              #line hidden
Line 152:  EndContext("~/Views/Home/About.cshtml", 286, 19, false);
Line 153:  
Line 154:  BeginContext("~/Views/Home/About.cshtml", 305, 11, true);
Line 155:  
Line 156:  WriteLiteral(";\r\n        ");
Line 157:  
Line 158:  EndContext("~/Views/Home/About.cshtml", 305, 11, true);
Line 159:  
Line 160:  BeginContext("~/Views/Home/About.cshtml", 348, 239, true);
Line 161:  
Line 162:  WriteLiteral("\r\n    </p>\r\n\r\n    <p>\r\n        Use this area to provide additional information.\r\n" +
Line 163:  "    </p>\r\n</article>\r\n\r\n<aside>\r\n    <h3>Aside Title</h3>\r\n    <p>\r\n        Use " +
Line 164:  "this area to provide additional information.\r\n    </p>\r\n    <ul>\r\n        <li>");
Line 165:  
Line 166:  EndContext("~/Views/Home/About.cshtml", 348, 239, true);
Line 167:  
Line 168:  BeginContext("~/Views/Home/About.cshtml", 588, 40, false);
Line 169:  
Line 170:              
Line 171:              #line 34 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 172:         Write(Html.ActionLink("Home", "Index", "Home"));
Line 173:  
Line 174:              
Line 175:              #line default
Line 176:              #line hidden
Line 177:  EndContext("~/Views/Home/About.cshtml", 588, 40, false);
Line 178:  
Line 179:  BeginContext("~/Views/Home/About.cshtml", 628, 19, true);
Line 180:  
Line 181:  WriteLiteral("</li>\r\n        <li>");
Line 182:  
Line 183:  EndContext("~/Views/Home/About.cshtml", 628, 19, true);
Line 184:  
Line 185:  BeginContext("~/Views/Home/About.cshtml", 648, 41, false);
Line 186:  
Line 187:              
Line 188:              #line 35 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 189:         Write(Html.ActionLink("About", "About", "Home"));
Line 190:  
Line 191:              
Line 192:              #line default
Line 193:              #line hidden
Line 194:  EndContext("~/Views/Home/About.cshtml", 648, 41, false);
Line 195:  
Line 196:  BeginContext("~/Views/Home/About.cshtml", 689, 19, true);
Line 197:  
Line 198:  WriteLiteral("</li>\r\n        <li>");
Line 199:  
Line 200:  EndContext("~/Views/Home/About.cshtml", 689, 19, true);
Line 201:  
Line 202:  BeginContext("~/Views/Home/About.cshtml", 709, 45, false);
Line 203:  
Line 204:              
Line 205:              #line 36 "c:\users\antonio.borrillo\documents\visual studio 2012\mvc3_w3cschool_example\modelsTests\modelsTests\Views\Home\About.cshtml"
Line 206:         Write(Html.ActionLink("Contact", "Contact", "Home"));
Line 207:  
Line 208:              
Line 209:              #line default
Line 210:              #line hidden
Line 211:  EndContext("~/Views/Home/About.cshtml", 709, 45, false);
Line 212:  
Line 213:  BeginContext("~/Views/Home/About.cshtml", 754, 26, true);
Line 214:  
Line 215:  WriteLiteral("</li>\r\n    </ul>\r\n</aside>");
Line 216:  
Line 217:  EndContext("~/Views/Home/About.cshtml", 754, 26, true);
Line 218:  
Line 219:          }
Line 220:      }
Line 221:  }
Line 222:  

Remove the semi colon from the end of the statement, it should be: @model modelsTests.Models.CompanyInfo

D'oh! Schoolboy error! Sorry.

Ah one more thing actually, sorry. With this statement

var company = new CompanyInfo
            {
                Name = "Company X",
                Description = "Company X does A, B, C and D.",
            };

we are effectively instantiating an object of class CompanyInfo, aren't we? But usually, wouldn't we do something like this instead:
var company = new CompanyInfo("Company X", "Company X does A, B, C and D");?

wouldn't we do

You can do that if you provide a constructor that accepts those two parameters:

public class CompanyInfo
{
    public string Name { get; set; }
    public string Description { get; set; }

    public CompanyInfo(string name, string description)
    {
        Name = name;
        Description = description;
    }
}

What you are using is an object initializer, here's an MSDN page about it.

Yes sure, that providing we have a constructor. But then, what's the difference between the two ways? To me, it makes more sense to use a constructor rather than the other syntax, but the author of the book I took the example from has decided not to. Is there any advantage in choosing the above over the one with constructors?

The object initializer route does allow you to assign arbitary values to one or more properties without having to define constructors for each permutation, which in my mind is one advantage to using them.

You could argue that by providing constructors would allow you to have some logic (maybe validation logic) within the constructor but then this should be in the setters of the properties unless that logic depended on other properties being set, which is where a constructor could be very useful.

To me, it makes more sense to use a constructor

For me too in most cases.

Is there any advantage

I don't think so (I haven't looked at the IL though). The object initializer way is just a shortcut for creating a class and setting properties. The upside is that you don't have to create specific constructors if all you want to do is set some properties, it just adds a little flexibility IMO.

OK thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.