0

I know this has been asked many times but I can't solve my problem which is very funny.

My model is simple:

public class RegisterModel
    {
        [Display(Name = "First name")]
        [Required]
        public string FirstName { get; set; }
        [Display(Name = "Last name")]
        [Required]
        public string LastName { get; set; }

        [Display(Name = "Email address")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Enter valid e-mail")]
        [Required(ErrorMessage = "E-mail is empty")]
        public string EmailAddress { get; set; }

        public System.Web.Mvc.SelectList Countries { get; set; }
    }

Action:

[AllowAnonymous]
        public virtual ActionResult Register(RegisterModel data)
        {
            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                ModelState.Clear();

                var countries =
                    this.ClientRepositoryBuilder
                        .CountryClientRepository
                        .GetAllCountries();

                data.Countries = new SelectList(countries, "Id", "CountryName");

                return
                    this.View(data);
            }
            else
            {
                return
                    this.RedirectToAction(MVC.Home.Index());
            }

        }

As soon as I add Countries into my model, stuff stops working and it doesn't invoke POST Action, give me an error with firebug(it's ajax post):

No parameterless constructor defined for this object

Win
  • 61,100
  • 13
  • 102
  • 181
sensei
  • 7,044
  • 10
  • 57
  • 125
  • Mostly likely Exception is thrown by DI in Controller's constructor. – Win Mar 25 '14 at 21:50
  • Sounds like it can't instantiate the Ajax controller's constructor. Can you post that here? – Jerad Rose Mar 25 '14 at 21:52
  • I think it's the `Countries` property of your RegisterModel. Could your DI is trying to inject that? Comment it out (just for testing) and try to POST. – Jeremy Cook Mar 25 '14 at 21:53
  • Consider having the type of `Countries` be `IEnumerable` instead of `SelectList` – Jeremy Cook Mar 25 '14 at 21:58
  • Hi thanks for responses. @JeremyCook if I use that type then it doesn't pass any value... Amount is 0 always. It doesn't invoke Post action when I include countries, but it invokes without. Very strange, just because of one SelectItem. – sensei Mar 26 '14 at 10:43
  • Check to see if it is going to the right action. If you have two actions with the same name, the request may be going to the other action. I would expect this action to have `[HttpPostAttribute]` on it. – Jeremy Cook Mar 26 '14 at 14:42

2 Answers2

0

Default Model binding can't deal with that object when trying to convert from information coming in request to RegisterModel.

Options:

If you want something like accepting list as request parameter - Model Binding to a List MVC 4 may have an answer.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Ok I did a mistake with type, don't even know how I did this mistake.. So I updated model with proper type for Countries and stuff works now:

[Required]
public int Countries { get; set; }

It was a mistake due mine tiredness ;)

sensei
  • 7,044
  • 10
  • 57
  • 125