asp.net mvc - this call is ambiguous between the following methods or properties when using 2 models in a view c# razor -


i having trouble using 2 models in view in mvc. when try reference second model using razor giving me error:

the call ambiguous between 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>>)' , '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>> 

this how call model in view:

@model ienumerable<myapp.models.problems> 

this model being loaded in view:

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

these other 2 models inside model:

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

and how trying call model inside of problems model using razor:

@foreach(problems problems in model) {    foreach(category category in problems.categories)    {       @html.displaynamefor(cat => @category.title)    }             } 

there error getting on line:

@html.displaynamefor(cat => @category.title) 

any me solve problem amazing, i've spent lot of time researching , can't seem find proper solution. thank in advance!

edit add controller:

so happens button clicked , trying populate modal razor information.

this using in controller:

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

this button kicks off java script create modal trying add to(it bring in problem id modal):

<button class="modal" value= @item.id role="button">add problem</button> 

along javascript modal being created:

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

when form submitted in modal sends in hardcoded radio buttons values controller method:

[httppost] public actionresult addproblem(problems model) {      list<category> c = db.categories.tolist();      list<type> t = db.types.tolist();       return view(); } 

this method when page loaded:

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) restresponse;              list<problems> problems = jsonconvert.deserializeobject<list<problems>>(responseprob.content);             ienumerable<problems> problemstoreturn = problems.where(d => d.projectid == item.projectid);              return view(problemstoreturn);         } 

change line:

@html.displaynamefor(cat => @category.title) 

to

@html.displaynamefor(cat => category.title) 

update

you need configure relations 1 many on models.

this model being loaded in 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;} } 

these other 2 models inside model:

public partial class category {  public int categoryid { get; set; }  public string title { get; set; }   public virtual myapp.models.problems problem { get; set; }//add line respect 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 line respect one-many config } 

source:configure-one-to-many-relationship-in-code-first


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -