0

I am beginner in .NET and I am learning MVC pattern. I am building a demo application which demonstrate CRUD Operations on any product. Before I use any database connectivity, I decided to test it with in memory variable. My code is as below:

Model Class

public class Product
{
    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name="Name")]
    public String ProductName { get; set; }

    [Required]
    [Display(Name="Price")]
    public float Price { get; set; }

    [Display(Name="Discription")]
    public String ProductDiscription { get; set; }
}

Controller Class

public class ProductController : Controller
{
    //
    // GET: /Product/
    public ActionResult Index()
    {
        return View(new Product{Id = 01, Price=100, ProductName="MyProduct", ProductDiscription="30 TeaBags in a pack"});
    }
}

View

    @model CrudAppDemo.Models.Product

@{
    ViewBag.Title = "Products";
}

<h2>Products</h2>

<div class="row">
    <div class="col-md-4 ">

        <table class="table">

            <thead>
                <tr>
                    <td>@Html.DisplayNameFor(model => model.Name)</td>
                    <td>@Html.DisplayNameFor(model => model.Price)</td>
                    <td>@Html.DisplayNameFor(model => model.Description)</td>
                </tr>
            </thead>

            <tbody>
                    <tr>
                        <td>@Html.DisplayFor(model => model.Name)</td>
                        <td>@Html.DisplayFor(model => model.Price)</td>
                        <td>@Html.DisplayFor(model => model.Description)</td>                        
                    </tr>
            </tbody>

        </table>

    </div>
</div>

When I am running this code, I am getting CS0411 Error:

enter image description here

4
  • what a silly mistake.....i was using display name instead of property name in view. thanks guys...finally got it worked... Commented Apr 28, 2017 at 9:24
  • but i didn't get it, why visual studio was not showing any hint when i type '.' (dot) after model...it should show property name as usual Commented Apr 28, 2017 at 9:26
  • Visual studio shows all property when you press.(dot) Commented Apr 28, 2017 at 9:28
  • @halfer , Thanks buddy... Commented Apr 28, 2017 at 11:01

2 Answers 2

1

Your parameter doesn't match the ones in model. you have ProductName in model and you are trying to access Name which is not in model, same goes for description.

Write this instead.

<thead>
                <tr>
                    <td>@Html.DisplayNameFor(model => model.ProductName)</td>
                    <td>@Html.DisplayNameFor(model => model.Price)</td>
                    <td>@Html.DisplayNameFor(model => model. ProductDiscription)</td>
                </tr>
            </thead>

            <tbody>
                    <tr>
                        <td>@Html.DisplayFor(model => model.ProductName)</td>
                        <td>@Html.DisplayFor(model => model.Price)</td>
                        <td>@Html.DisplayFor(model => model. ProductDiscription)</td>                        
                    </tr>
            </tbody>
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you are using the display name [Display(Name="Name")] as your property rather than the property itself public String ProductName { get; set; }. Try changing to use the property name.

  <td>@Html.DisplayNameFor(model => model.ProductName)</td>
  <td>@Html.DisplayNameFor(model => model.Price)</td>
  <td>@Html.DisplayNameFor(model => model.ProductDiscription)</td>

Comments

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.