传入字典的模型项的类型为' my_photos.Models.profile',但需要输入' my_photos.Models._partial

时间:2015-12-18 07:29:18

标签: asp.net-mvc asp.net-mvc-4

您好我在那里更新我的问题所以有人可以在那里获得更多详细信息我的应用程序中发生了什么,因为我告诉您我是.net mvc环境的新手。所以这是我第一次在不同的时间创建我的应用程序。我创建了三个模型,并将它们放在仪式的地方。当我从局部视图应用程序评论模型运行正常。但如果我取消注释模型应用程序返回以下错误。 [InvalidOperationException:部分视图'〜/ Views / Shared / Partial.cshtml'找不到或没有视图引擎支持搜索的位置。搜索了以下位置 这是我的_layout.cshtml文件。

    @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>

这是我的主控制器文件。

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);
        }

    }
}

这是关于我的文件。

@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>

这是我的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>

以下是我的模特

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; }
}

当我运行此代码时,它返回:

  

传递到字典中的模型项的类型为&#39; my_photos.Models.profile&#39;,但此字典需要类型为&#39; my_photos.Models._partial&#39;的模型项。

请帮助我

1 个答案:

答案 0 :(得分:1)

我发现您有一个要使用的操作_partial,这意味着您必须@Html.Action而不是Html.Partial

首先,您必须从布局中删除@model my_photos.Models._partial。这将允许其他视图使用不同的模型,而不仅限于一个(由布局使用)。

其次,您必须替换布局@Html.Partial(..)中的@Html.Action("_partial", "Default")。这将通过从控制器Default。

调用操作_partial来呈现您的部分

生成的布局如下所示:

@* 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>