문제

Getting an error I don't understand : "The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Decimal'." Happens at this line in the View :

<td>@Html.EditorFor(model => model.Price)</td>

It should be null. This is for a Create Product page. Here's the code :

public ActionResult Create()
        {           
            var items = new ProductItems();
            return View(items.Products);

VIEW :
@using Nop.Web.Models.Products
@model ProductItems

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)    
 <tr>
     <td>@Html.EditorFor(model => model.Price)  << error here

VIEW MODEL :
 public class ProductItems
    {
        public decimal Price { get; set; }
        public IEnumerable<Product> Products { get; set; } 


NOTE : I previously changed the View Model. It was :
@model Nop.Core.Domain.Catalog.Product
and it worked before I changed it.

What's causing this error? thanks

도움이 되었습니까?

해결책

You are not passing anything to the view but an empty object, hence why it is saying model.price is null. If you want your editor template to be populated you must say

public ActionResult Create()
{           
            ProductItems items = new ProductItems();
            items.Price = 15.99;
            return View(items);

 }

Since you are passing the view a model of type ProductItems your view must also expect that type. You should be putting your Model Classes in to the Models folder. Default values to call in your view would be

@model NameSpace.Models.ProductItems, not sure of the path to your model class.

다른 팁

@model  Nop.Web.Models.ProductItems

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)    
    <table>
        <tr>
            <td>@Html.EditorFor(model => model.Price)
            </td>
        </tr>

    </table>
}

i have use your code and your code not raise error in my processing but you also use like this view and be careful first clean build .

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top