C# 没有给出与所需形式参数';id';属于';Human.Human(int,string,string,string)和#x27;
我无法从HomeController操作方法调用新的人类对象C# 没有给出与所需形式参数';id';属于';Human.Human(int,string,string,string)和#x27;,c#,.net,C#,.net,我无法从HomeController操作方法调用新的人类对象 var employee = new Human { id = 1, name = "home index" , isAuth = isAuth,token="null"}; 这是我的模型 namespace WebApplication3.Models { public class Human { public Human(int id, string name, string is
var employee = new Human { id = 1, name = "home index" , isAuth = isAuth,token="null"};
这是我的模型
namespace WebApplication3.Models
{
public class Human
{
public Human(int id, string name, string isAuth, string token)
{
this.id = id;
this.name = name;
this.isAuth = isAuth;
this.token = token;
}
试试这个
var employee = new Human(1, "home index" isAuth, null);
您正在使用的语法是,但此语法要求至少使用空构造函数,因为大括号中的代码是在构造对象之后执行的 如果要使用该语法,需要将emtpy构造函数添加到模型中
public class Human
{
public Human() {}
public Human(int id, string name, string isAuth, string token)
{
this.id = id;
this.name = name;
this.isAuth = isAuth;
this.token = token;
}
现在你有两个选择。使用当前语法,以便编译器调用空构造函数,然后使用大括号之间的值初始化属性,或者使用正常语法直接使用四个参数调用构造函数
var employee = new Human (1,"home index",isAuth,"null");
请注意,对于第一种方法,您需要将这些属性公开
public class Human
{
public int id {get;set;}
.....
它应该像新人类一样(id=1,name=“home index”,isAuth=isAuth,token=“null”);在构造函数中使用圆括号代替大括号。或者在@Gaurav中添加一个空构造函数(前提是这些属性是公共的),您不能使用“=”来命名参数。
var employee=newhuman(id:1,name:“home index”,isAuth:isAuth,token:“null”)代码>或只是var employee=new Human(1,“home index”,isAuth,“null”)代码>或var employee=newhuman(id:1,名称:“home index”,isAuth:isAuth,令牌:null)如果代码>有助于可读性,则使用冒号。