使用不同控制器和模型的MVC共享部分视图

时间:2013-09-25 13:52:33

标签: c# asp.net-mvc asp.net-mvc-4 controller partial-views

我有2个控制器,可生成2个索引视图。 我想要做的是将这些视图用作全局共享的部分视图,但似乎无法使其正常工作。

有人知道这是否可能吗?

我的控制器代码是

  public ActionResult Index()
        {

            var viewModel = (from P in db.Projects
                             join R in db.Reports on P.ProjectTitle equals R.ReportProjectID into ps
                             from R in ps.DefaultIfEmpty()
                             select new MyViewModel { Project = P, Report = R });


            return View(viewModel);
        }

我的ViewModel代码是

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace MiLife2.ViewModels
    {
    public class MyViewModel
    {
        public Project Project { get; set; }
        public Report Report { get; set; }
    }
    }

我的观点是

    @model IQueryable<MiLife2.ViewModels.MyViewModel>

    @{
    ViewBag.Title = "Index";
    }
    enter code here

<h2>Index</h2>
<div>@Html.Partial("_Partial1")</div>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>@item.Project.ProjectTitle </td>
        <td>@item.Project.ProjectCreatedByID</td>
        <td>@item.Project.ProjectCreatedDate</td>


        <td>@if (item.Report == null)
            {
                <text>No Reports</text>
            }
            else
            {
               @item.Report.Title;
            }
        </td>
        <td>@if (item.Report == null)
            {
                <text> </text>
            }
            else
            {
               @item.Report.Description;
            }</td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Project.ProjectID }) |
            @Html.ActionLink("Details", "Details", new {  id=item.Project.ProjectID }) |
            @Html.ActionLink("Delete", "Delete", new {  id=item.Project.ProjectID })
        </td>
    </tr>
}

</table>

如果我创建了一个部分页面并将上面的视图粘贴到其中,然后使用@ HTML.Partial(“_ ProjPartial”)我收到错误

传递到字典中的模型项的类型为'System.Collections.Generic.List 1[MiLife2.Project]', but this dictionary requires a model item of type 'System.Linq.IQueryable 1 [MiLife2.ViewModels.MyViewModel]'。

如果我在特定控制器视图文件夹的Ind​​ex cshtml页面中使用@ HTML.Partial(“_ ProjPartial”),则不会发生这种情况。

2 个答案:

答案 0 :(得分:3)

从错误中我看起来你的局部视图正在寻找与你的视图相同的模型。将模型传递给部分应修复该错误

@Html.Partial("_Partial1", Model)

更新:

因为那不适合你,我会尝试使用ajax调用

$('.btnSubmit').on('click', function(){
    $.ajax({
         url: "@(Url.Action("Action", "Controller"))",
         type: "POST",
         cache: false,
         async: true,
         data: { id: id },
         success: function (result) {
             $(".Content").html(result);
         }
    });

 });

然后在您的控制器中

public PartialViewResult GetPartial()
    {

        var viewModel = (from P in db.Projects
                         join R in db.Reports on P.ProjectTitle equals R.ReportProjectID into ps
                         from R in ps.DefaultIfEmpty()
                         select new MyViewModel { Project = P, Report = R });


        return PartialView("_Partial1", viewModel);
    }

使用此ajax调用,您可以从任何视图调用局部视图,您可以在按钮单击时或根据需要传递不同的ID以刷新视图。希望以这种方式调用它将修复您的错误。如果您有任何问题,请与我联系。

答案 1 :(得分:0)

最近碰到了类似的东西,所以我想加2美分。对我来说,答案就在于我传递给部分视图。

我试图将一个字符串传递给局部视图,但是当该字符串碰巧是null时,它表现得好像我没有将任何东西传递给Partial,这意味着它默认传递给了当前视图的模型。

例如,我有一个视图,它呈现部分和部分接受字符串:

@model SomeModel

@{ Html.RenderPartial("_MyPartialView", SomeModel.StringProperty) }

如果SomeModel.StringProperty恰好是null,那么它将尝试传递当前视图的模型(在这种情况下为SomeModel)。所以相反,我只是写了下面的内容,如果SomeModel.StringProperty恰好是null,它将传入一个空字符串:

@model SomeModel

@{ Html.RenderPartial("_MyPartialView", SomeModel.StringProperty ?? string.Empty) }

希望这有助于某人。