c# 異なるコントローラとモデルを使用した MVC 共有部分ビュー
mvc asp net view model (2)
私は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.Project]', but this dictionary requires a model item of type 'System.Linq.IQueryable
1 [MiLife2.ViewModels.MyViewModel]' 1[MiLife2.Project]', but this dictionary requires a model item of type 'System.Linq.IQueryable
。
特定のコントローラビューフォルダのIndex cshtmlページ内で@ HTML.Partial( "_ ProjPartial")を使用すると、これは起こりません。
https://ffff65535.com
このエラーから、あなたの部分的な視点は、あなたの視点と同じモデルを探しているように見えます。 モデルをパーシャルに渡すとそのエラーを修正するはずです
@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、ボタンのクリック、または必要に応じてビューを更新することができます。 うまくいけば、この方法であなたのエラーを修正することができます。 ご質問がある場合はお知らせください。
最近似たようなものになったので、私は2セントを追加したかったのです。 私の答えは、私がパーシャルビューに渡していたものでした。
部分的なビューに文字列を渡そうとしていましたが、その文字列がnull
になったとき、Partialに何も渡されなかったかのように振る舞いました。つまり、デフォルトで現在のビューのモデルを渡します。
たとえば、私は部分的にレンダリングするビューを持ち、部分的には文字列を取ります:
@model SomeModel
@{ Html.RenderPartial("_MyPartialView", SomeModel.StringProperty) }
SomeModel.StringProperty
がnull
場合、現在のビューのモデル(これはSomeModel
)をSomeModel
ます。 そうではなく、 SomeModel.StringProperty
がnullの場合、空の文字列で渡す以下のSomeModel.StringProperty
。
@model SomeModel
@{ Html.RenderPartial("_MyPartialView", SomeModel.StringProperty ?? string.Empty) }
これが誰かを助けることを望みます。