1

Check MVC code bellow. I have created this controller but i want to add a Model with this controller to Retrieve user email list and id. But problem is all online tutorial says create model during create controller since i already created controller i have to create model manually now. Please advice me any solution how can i Create model for this controller? Please also note i have to access that Model from "SubmitAction" method on this controller

    namespace WebApplication2.Controllers
    {
        [AdministratorOnlyPermission]
        public class EmailCampaignController : Controller
        {

            // GET: Email Campaign
            public ActionResult Index()
            {
                return View();
            }

            public static IRestResponse SendSimpleMessage()
            {
                RestClient client = new RestClient();
                client.BaseUrl = new Uri("https://api.mailgun.net/v3");
                client.Authenticator =
                    new HttpBasicAuthenticator("api",
                                                "key-34d6fd732e9ba43a2fakeeeeeee4b60c");
                RestRequest request = new RestRequest();
                request.AddParameter("domain", "sandboxc8436ac0db054b45986303cb8fd3944a.mailgun.org", ParameterType.UrlSegment);
                request.Resource = "{domain}/messages";
                request.AddParameter("from", "Excited User <[email protected]>");
                request.AddParameter("to", "[email protected]");
                request.AddParameter("subject", "Hello");
                request.AddParameter("text", "Testing some Mailgun awesomness!");
                request.Method = Method.POST;
                return client.Execute(request);
            }



            [HttpPost]
            public ActionResult SubmitAction(FormCollection collection)
            {
                //var userType = Request["userType"];
                //var emailContent = Request["emailContent"];
                //SendSimpleMessage();
                //ViewBag.Message = "Hello " + Request["userType"];

            }

        }
    }

1 Answer 1

1

Depending on what your form is submitting to the controller, your model will vary.

Let's say you were using FormCollection as the parameter in your SubmitAction like below:

public ActionResult SubmitAction(FormCollection collection)

And let's further say your form had controls with the following names and you were getting these items from the FormCollection:

  1. Name
  2. Age

Now you want to use a model instead. All you have to do is to create a class that has those properties and call the class whatever you like:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

The MVC DefaultBinder will take the items you are submitting in your form, take the name of the form controls and look for those in your Person class, if it finds a property with a matching name, it will set that property of the Person class. It will do that for all the controls in the form.

You will change the signature of the action to accept the model as parameter like this:

public ActionResult SubmitAction(Person person)
Sign up to request clarification or add additional context in comments.

4 Comments

But "Name" and "Age" are column names. How entityframework will select the table name? is the "person" is table name? or what?
@TeenaR Like I said, you can call the class whatever you want. Plus you never mentioned about EF. You will take the thing coming into the action, such as Person, and then from that create the thing you want to add to the database.
sorry mightbe bad question. if i don't say on which table operation should be happen then how system will know that? i'm confused here. suppose: i have two different table contains same column called "Age" then system will confused about table isn't it?
You are confusing models (in MVC) with and Entity Model. Let's continue with my example of Person. That is a model (not an entity model). You can take that and create your entity model from that and your entity model can be called Customer or anything. You can also use your entity model in your action if you want. Some people call the mvc model (view model).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.