casper
casper

Reputation: 45

the model item passed into the dictionary is of type 'my_photos.Models.profile', but need type 'my_photos.Models._partial

hi there i am updating my question there so someone could get more details over there what exactly happening there in my app as i told you i am new with the .net mvc environment. so this is my vary first time that i am creating some of my app in it.i have created three models and have place them in the rite place. when i comment model from the partial view application runs fine. but if i uncomment the model application return the following error. [InvalidOperationException: The partial view '~/Views/Shared/Partial.cshtml' was not found or no view engine supports the searched locations. The following locations were searched here is my _layout.cshtml file.

    @model my_photos.Models._partial
    @{
        Layout = null; 
    }
    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>_Layout</title>
        <link href="@Url.Content("~/Content/Style/Site.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/Style/Side_barnav.css")" rel="stylesheet" type="text/css" />


    </head>
        <body>
            <header>
                <h3> Photos app</h3>
                <p> In this app my major concerns are with the <strong>theaming</strong>!</p>
                <div class="nav">
                <ul class="main-menu">
                    <li>@Html.ActionLink("Home","Index", "Default")</li>
                    <li>@Html.ActionLink("About","About", "Default")</li>
                    <li>@Html.ActionLink("Contact","Contact", "Default")</li>
                </ul>
                </div>
            </header>
        <section>
                @RenderBody()
                @Html.Partial("~/Views/Shared/_partial.cshtml", Model)
        </section>
            <footer>
            <div class="fotter-menu">
                <ul>
                    <li>@Html.ActionLink("Home","Index","Default")</li>
                    <li>@Html.ActionLink("About","About","Default")</li>
                    <li>@Html.ActionLink("Contact","Contact","Default")</li>
                </ul>
                <ul>
                    <li>@Html.ActionLink("Resources","Resources","Default")</li>
                    <li>@Html.ActionLink("Testimonial","Testimonial","Default")</li>
                    <li>@Html.ActionLink("Team","Team","Default")</li>
                </ul>
                <ul>
                    <li>@Html.ActionLink("Home","Index","Default")</li>
                    <li>@Html.ActionLink("Home","Index","Default")</li>
                    <li>@Html.ActionLink("Home","Index","Default")</li>
                </ul>
            </div>
        </footer>
    </body>
</html>

and here is my main controller file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using my_photos.Models;


namespace my_photos.Controllers
{
    public class DefaultController : Controller
    {


        public ActionResult Index()
        {
            ViewBag.Massage = "Hello Word";
            var Profile = new profile() { 
                name = "Aston",
                father_name = "Johan Deoh",
                address = " XYZ XYZ XYZ XYZ",
                Phone = "123123123",
                age = 22
            };

            return View(Profile);
        }
        public ActionResult About() {

            var aboutus = new about() {
                Us = "this is all i got by the way"
            };
            return View(aboutus);
        }


        public ActionResult _partial()
        {
            var model = new _partial() { 
                Winner = "Shafee Jan"
            };
            return PartialView("_partial",model);
        }

    }
}

Here is my about file.

@model my_photos.Models.about
@{
    ViewBag.Title = "About";
}
 <div class="content-container clear-fix">
    <h1 class="heading-width">About-Page</h1>
    <p class="paragraph"> 
        @Model.Us
    </p>
</div>

Here is my index.cshtml

@model my_photos.Models.profile
@{
    ViewBag.Title = "Index"; 
}
<div class="content-container clear-fix">
    <h1 class="heading-width">Names </h1>
    <ul>
        <li> name @Model.name.ToString()</li>
        <li> father name: @Model.father_name.ToString()</li>
    </ul>
</div>

Here are my models

public class _partial
{
    public string Winner { get; set; }
}

public class about
{
    public string Us { get; set; }
}

public class profile
{
    public String name { get; set; }
    public String father_name { get; set; }
    public String Phone { get; set; }
    public String address { get; set; }
    public String Status { get; set; }
    public int age { get; set; }
}

when ever i run this code it returns:

The model item passed into the dictionary is of type 'my_photos.Models.profile', but this dictionary requires a model item of type 'my_photos.Models._partial'.

kindly help me

Upvotes: 0

Views: 66

Answers (2)

Deepak
Deepak

Reputation: 119

use HTML Action

Use  @Html.Action("_partial", "Controler");

Upvotes: 1

adricadar
adricadar

Reputation: 10219

I see that you have an action _partial that you want to use, that means you have to @Html.Action not Html.Partial.

First you have to remove @model my_photos.Models._partial from Layout. This will allow other views to use different model and to not be restricted to just one (used by layout).

Second you have to replace @Html.Partial(..) in Layout @Html.Action("_partial", "Default"). This will render your partial by calling the action _partial from the controller Default.

The resulting Layout will look like this:

@* no more model here *@   
@{
    Layout = null; 
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>_Layout</title>
    <link href="@Url.Content("~/Content/Style/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/Style/Side_barnav.css")" rel="stylesheet" type="text/css" />


</head>
    <body>
        <header>
            <h3> Photos app</h3>
            <p> In this app my major concerns are with the <strong>theaming</strong>!</p>
            <div class="nav">
            <ul class="main-menu">
                <li>@Html.ActionLink("Home","Index", "Default")</li>
                <li>@Html.ActionLink("About","About", "Default")</li>
                <li>@Html.ActionLink("Contact","Contact", "Default")</li>
            </ul>
            </div>
        </header>
    <section>
            @RenderBody()
            @Html.Action("_partial", "Default") @* Changed *@
    </section>
        <footer>
        <div class="fotter-menu">
            <ul>
                <li>@Html.ActionLink("Home","Index","Default")</li>
                <li>@Html.ActionLink("About","About","Default")</li>
                <li>@Html.ActionLink("Contact","Contact","Default")</li>
            </ul>
            <ul>
                <li>@Html.ActionLink("Resources","Resources","Default")</li>
                <li>@Html.ActionLink("Testimonial","Testimonial","Default")</li>
                <li>@Html.ActionLink("Team","Team","Default")</li>
            </ul>
            <ul>
                <li>@Html.ActionLink("Home","Index","Default")</li>
                <li>@Html.ActionLink("Home","Index","Default")</li>
                <li>@Html.ActionLink("Home","Index","Default")</li>
            </ul>
        </div>
    </footer>
</body>

Upvotes: 1

Related Questions