简体   繁体   中英

Strange error: No parameterless constructor defined for this object

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

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.

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 ;)

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