Zholen
Zholen

Reputation: 1790

How should I setup Registration controller action?

I am fairly new to MVC and i'm looking for advice on how to setup a particular registration controller.

I have a controller called AccountController which has a Register method and I have a Register.cshtml.

Now, one of the biggest problems I seem stuck on is that I have 2 dropdowns that I need to populate based on the response from a service as these values change depending on location and other various parameters.

I have my page started and loading but I'm not sure what to do once a user click 'register'.

@model Adw.Models.RegisterModel //This is my model

@Html.DropDownListFor(m => m.States, new SelectList(Model.States)); // I load my dropdowns here

[AllowAnonymous]
public ActionResult Register(RegisterModel model)
{
    model.States = Services.GetStates().Payload;
    model.Countries = Services.GetCountries().Payload;
    return View(model);
}

So my question is, when a user submits the form, should it come back to this same method? If so what would be the best way to validate that this is a submit rather than an initial load?

Also i haven't done much in the way of error handling and could use a suggestion on that, such as if either of the above service calls fail, then a registration cannot be completed, should that direct to a new page or is there a easy way to build that kind of error into the same page?

Upvotes: 0

Views: 48

Answers (1)

Sławomir Rosiek
Sławomir Rosiek

Reputation: 4073

You should create two different method. One for GET and second for POST request:

[AllowAnonymous]
[HttpGet]
public ActionResult Register()
{
    ...
}

[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // create user

        return this.RedirectToAction("SignIn");
    }
    else
    {
        return View(model);
    }
}

You can review sample from default template.

Upvotes: 1

Related Questions