简体   繁体   中英

How can I call my model from controller?

I am attempting to call my model from my controller, but get errors. The error is on

new UserModels(id, searchcriteria);

and states that

UserModels does not contain a constructor that takes 2 arguments.

Any ideas?

Controller/Action:

    public ActionResult ID(string id)
    {
        ViewBag.Message = "Customer Information";

        string searchcriteria = "userid";

        UserModels model = new UserModels(id, searchcriteria);

        return View();
    }

Model:

public class UserModels
    {
        public UserData user { get; set; }

        public string firstname { get; set; }
        public string lastname { get; set; }

        public string searchvalue {get; set; }
        public string searchcriteria { get; set; }


        public List<UserData> UserModel(string id, string searchcriteria) 
        {
            SSO_Methods sso = new SSO_Methods();

            List<UserData> userObject = sso.GetUserObject(id, searchcriteria);

            return userObject;

        }





    }

Constructors in c# cannot return anything.

Your code would need to be

public UserModels(string id, string searchcriteria) 
{
    // your code here
}

Then if you are wanting to return a list, add in

public List<UserData> GetUserModels(string id, string searchcriteria) 
{    
    SSO_Methods sso = new SSO_Methods();
    List<UserData> userObject = sso.GetUserObject(id, searchcriteria);
    return userObject;
}

You have to create a constructor which has 2 parameters. To create a constructor you have to write something like this: [public, private, protected, internal] [classname]([parameters]) . So just change this: public List<UserData> UserModel(string id, string searchcriteria) To public UserModel(string id, string searchcriteria) . Notice that a constructor must not return anything. Use properties instead of your return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM