asp.net mvc-当在视图c#razor中使用两个模型时,以下方法或属性之间的调用是不明确的
本文关键字:模型 方法 属性 不明确 调用 之间 两个 视图 mvc- net c#razor | 更新日期: 2023-09-27 18:00:05
我在mvc视图中使用两个模型时遇到问题。当我试图使用剃须刀引用第二个模型时,它给了我这个错误:
The call is ambiguous between the following methods or properties: 'System.Web.Mvc
.Html.DisplayNameExtensions.DisplayNameFor<System.Collections.Generic.IEnumerable
<MyApp.Models.Problems>, string>(System.Web.Mvc.HtmlHelper<System.Collections.
`Generic.IEnumerable<MyApp.Models.Problems>`(System.Linq.Expressions.Expression
<System.Func<System.Collections.Generic.IEnumerable<MyApp.Models.Problems>,
string>>)' and 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor<MyApp
.Models.Problems>,string(System.Web.Mvc.HtmlHelper<Systems.Collections.Generic
.IEnumerable<MyApp.Models.Problems>>(System.Linq.Expressions.Expression<System.Func
<MyApp.Models.Problems>,string>>
这就是我在视图中对模型的称呼:
@model IEnumerable<MyApp.Models.Problems>
这是我在视图中加载的模型:
public class Problems
{
public int Id { get; set; }
public string Title { get; set; }
public IEnumerable<MyApp.Models.Category> Categories { get; set; }
public IEnumerable<MyApp.Models.Type> Types {get; set;}
}
这是该模型中的另外两个模型:
public partial class Category
{
public int Id { get; set; }
public string Title { get; set; }
}
public partial class Type
{
public int Id { get; set; }
public int Order { get; set; }
public string Description { get; set; }
}
这就是我试图使用剃刀将问题模型内部的模型称为的方式
@foreach(Problems problems in Model)
{
foreach(Category category in problems.Categories)
{
@Html.DisplayNameFor(cat => @category.Title)
}
}
我收到的错误是在这条线上:
@Html.DisplayNameFor(cat => @category.Title)
任何能帮我解决这个问题的帮助都会很神奇,我花了很多时间研究,似乎找不到合适的解决方案。提前谢谢!
编辑以添加控制器:
所以,点击一个按钮,我试图用剃刀信息填充一个模态。
这就是我在控制器中使用的:
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MyApp.DTOs;
using MyApp.Models;
public class ProblemController : BaseController
{
private Context db = new Context();
}
这是启动java脚本以创建我试图将其添加到的模态的按钮(它将问题Id带入模态):
<button class="modal" value= @item.Id role="button">Add Problem</button>
连同我为正在创建的模式创建的javascript:
<script>
$(function () {
$("#myModal").dialog({
autoOpen: false,
height: 600,
width: 500,
modal: true,
open: function () {
$("#accordion").accordion({ autoHeight: true });
}
});
$(".modal").click(function () {
defectId.value = $(this).attr('value');
$("#myModal").dialog("open");
})
});
</script>
当表单以模式提交时,它会将硬编码的单选按钮值发送给我的控制器,方法如下:
[HttpPost]
public ActionResult AddProblem(Problems model)
{
List<Category> c = db.Categories.ToList();
List<Type> t = db.Types.ToList();
return View();
}
这是我加载页面时的获取方法:
public ActionResult GetProblem(ProjectProblemVM item)
{
var userid = getUserId();
var userKey = getApiKey();
// return problems
var requestProb = new RestRequest("problem", Method.GET);
requestProb.AddHeader("id", userid);
requestProb.AddHeader("key", userKey);
requestProb.RequestFormat = DataFormat.Json;
var responseProb = Host.Execute(requestProb) as RestResponse;
List<Problems> problems = JsonConvert.DeserializeObject<List<Problems>>(responseProb.Content);
IEnumerable<Problems> problemsToReturn = problems.Where(d => d.ProjectId == item.ProjectId);
return View(problemsToReturn);
}
更改此行:
@Html.DisplayNameFor(cat => @category.Title)
至
@Html.DisplayNameFor(cat => category.Title)
更新
您还需要在所有模型上一对多地配置好关系。
This is my model being loaded in the view:
public class Problems
{
public Problems()
{
Categories = new List<MyApp.Models.Category>();
Types = new List<MyApp.Models.Type>();
}
public int ProblemId { get; set; }
public string Title { get; set; }
public IEnumerable<MyApp.Models.Category> Categories { get; set; }
public IEnumerable<MyApp.Models.Type> Types {get; set;}
}
这是该模型中的另外两个模型:
public partial class Category
{
public int CategoryId { get; set; }
public string Title { get; set; }
public virtual MyApp.Models.Problems Problem { get; set; }//Add this line to respect the one-many config
}
public partial class Type
{
public int TypeId { get; set; }
public int Order { get; set; }
public string Description { get; set; }
public virtual MyApp.Models.Problems Problem { get; set; }//Add this line to respect the one-many config
}
来源:在代码优先的中配置一对多关系