简体   繁体   English

奇怪的错误:没有为此对象定义无参数构造函数

[英]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): 一旦将“国家/地区”添加到模型中,东西就会停止工作,并且不会调用POST操作,请给我一个萤火虫错误(这是ajax帖子):

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 . 尝试将请求中的信息转换为RegisterModel时,默认模型绑定无法处理该对象。

Options: 选项:

If you want something like accepting list as request parameter - Model Binding to a List MVC 4 may have an answer. 如果您想要接受列表作为请求参数之类的内容- 到列表的模型绑定MVC 4可能会有答案。

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 ;) 这是我的疲倦所致的一个错误;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object 在MVC中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in MVC ModelBinding 抛出错误 No parameterless constructor defined for this object - ModelBinding throws error No parameterless constructor defined for this object 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object 错误消息“未为此对象定义无参数构造函数” - Error message “No parameterless constructor defined for this object” 自动映射器错误 - “没有为此对象定义无参数构造函数” - Auto Mapper Error - "No Parameterless constructor defined for this object" automapper 中没有为此 object 定义无参数构造函数 - No parameterless constructor defined for this object in automapper 在mvc中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in mvc MissingMethodException:没有为此对象定义无参数构造函数 - MissingMethodException: no parameterless constructor defined for this object 下拉菜单允许我遇到问题-没有为此对象定义无参数构造函数 - dropdown allows problems to I get error - No parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM