在视图c#razor中使用2个模型时,此调用在以下方法或属性之间不明确

时间:2014-04-25 00:17:23

标签: c# asp.net-mvc asp.net-mvc-4 razor

我在mvc的视图中使用2个模型时遇到问题。当我尝试使用剃刀引用第二个模型时,它给了我这个错误:

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

这就是我试图使用razor在问题模型中调用模型的方法:

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

这是加载页面时的get方法:

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

1 个答案:

答案 0 :(得分:4)

更改此行:

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

来源:configure-one-to-many-relationship-in-code-first