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

编程入门 行业动态 更新时间:2024-10-11 03:15:40
本文介绍了使用不同的控制器和模型的MVC共享部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have 2 controllers that generate 2 index views. What i would like to do is use these views as global shared partial views but cant seem to get this working.

有人知道这是否有可能吗?

Does anyone know if this is even possible?

我的控制器代码是

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代码是

My ViewModel code is

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")我会收到错误消息

If i create a partial page and paste the above view into it and then use @HTML.Partial("_ProjPartial") i get the error

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

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[MiLife2.Project]', but this dictionary requires a model item of type 'System.Linq.IQueryable1[MiLife2.ViewModels.MyViewModel]'.

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

This does not happen if i use @HTML.Partial("_ProjPartial") from within the Index cshtml page in the specific controller views folder.

推荐答案

从错误看来,我的部分视图正在寻找与视图相同的模型.将模型传递给您的局部模型应该可以解决该错误

From the error it looks like to me that your partial view is looking for the same model as you have on your view. Passing the model to your partial should fix that error

@Html.Partial("_Partial1", Model)

更新:

由于对您不起作用,我将尝试使用ajax调用

since that didn't work for you I would try using an ajax call

$('.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.希望以这种方式调用它可以解决您的错误.让我知道您是否有任何疑问.

Using this ajax call you can call the partial view from any view and you can pass different id's, on button clicks or as needed to refresh the view. Hopefully calling it this way will fix your error. let me know if you have any questions.

更多推荐

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

本文发布于:2023-08-03 14:27:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控制器   模型   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!