0

Error: InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Dynamic.ExpandoObject', but this ViewDataDictionary instance requires a model item of type 'MyProject.Models.Comments'.

This is My ViewResult:


    private RecepieDbContext _recepie;
    private UserDbContext _usr;
    private CommentsDbContext _cmnt;
    
    
        public RecepieController(RecepieDbContext contx,UserDbContext usr,CommentsDbContext cmntObj)
        {
            _recepie=contx;
            _usr=usr;
            _cmnt = cmntObj;
    
        }
    public ViewResult Details(int id)
        {
            dynamic mymodel = new ExpandoObject();
            var det = _recepie.Recepies.Find(id);
            var user = _usr.Users.Find(det.UserId);
            mymodel.Recepie=det;
            mymodel.User=user;
            return View(mymodel);
        }

This is My View Code named Details.cshtml:


    @* @model MyProject.Models.Recepie *@
    @* @using MultipleModelInOneView; *@
    @model dynamic
    
    @{
        Layout="_RecepieLayout";
    }
    
    <div class="row">
            <div class="col-md-5 col-sm-12" >
                <img src="~/Images/fishsoup.jpg" style="max-width:500px;"/>
            </div>
            <div class="col-md-7">
                <h1>
                    @Model.Recepie.Category
                </h1>
                <p>
                    @Model.Recepie.name
                </p>
                <p>
                    Created by @Model.User.name
                </p>
                <p>
                    @Model.Recepie.Description
                </p>
                <p>
                    @Model.Recepie.Ingredients
                </p>
    
            </div>
    </div>
    
    <partial name="Comments" />

And This is The Partial View fpr comments:


    @model MyProject.Models.Comments
    <div class="container">
        <form>
            <label asp-for="name"></label>
            <input asp-for="name" type="text"/>
            <label asp-for="email"></label>
            <input asp-for="email" type="text"/>
            <label asp-for="Comment"></label>
            <input asp-for="Comment" type="text-area"/>
        </form>
    </div>

1 Answers1

0

You need to provide a model instance to the Partial View with model attribute.

@using MyProject.Models;

<partial name="Comments" model="new Comments()" />
Yong Shun
  • 35,286
  • 4
  • 24
  • 46