Home @model giving error in asp.net mvc 5 view
Reply: 0

@model giving error in asp.net mvc 5 view

user2651
1#
user2651 Published in April 21, 2018, 4:00 am

I am new to asp.net MVC, I have following controller,Model & view and I used database first approach with EF and using Storedprocedure to return results.

After creating EF6, I am able to see my model under edmx page ( Not under models)

I have attached sample code

controller

 public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            RBACv3Entities test = new RBACv3Entities();
            var result =  test.testing();

            return View(result.ToList());

        }
    }

Model

public partial class test
    {
        public int id { get; set; }
        public string name { get; set; }
    }

View

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table align="center" border="1" cellpadding="4" cellspacing="4">
    <tr>
        <th style="background-color: Yellow;color: blue">Customer ID</th>
        <th style="background-color: Yellow;color: blue">Customer Name</th>
        <th style="background-color: Yellow;color: blue">City</th>
        <th style="background-color: Yellow;color: blue">Country</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.id</td>
            <td>@item.name</td>
            <td>NULL</td>
            <td>NULL</td>
        </tr>
    }
</table>  

If I use @model in top of my page in Razor view, I am getting error and not allowing to use @model.test in top but I can access all results without using these at top. When should I use @model in top of Razor.

error

CS0119: 'Mypro.test' is a 'type', which is not valid in the given context

View I used with @model

@model Mypro.test
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table align="center" border="1" cellpadding="4" cellspacing="4">
    <tr>
        <th style="background-color: Yellow;color: blue">Customer ID</th>
        <th style="background-color: Yellow;color: blue">Customer Name</th>
        <th style="background-color: Yellow;color: blue">City</th>
        <th style="background-color: Yellow;color: blue">Country</th>
    </tr>
    @foreach (var item in Mypro.test)
    {
        <tr>
            <td>@item.name</td>

        </tr>
    }
</table>  
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.3504 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO