Reputation: 456
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:
Upvotes: 0
Views: 613
Reputation: 69
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>
Upvotes: 1
Reputation: 487
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>
Upvotes: 1