簡體   English   中英

奇怪的錯誤:沒有為此對象定義無參數構造函數

[英]Strange error: No parameterless constructor defined for this object

我知道這個問題已經被問過很多次了,但是我無法解決我的問題,這很有趣。

我的模型很簡單:

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

行動:

[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());
            }

        }

一旦將“國家/地區”添加到模型中,東西就會停止工作,並且不會調用POST操作,請給我一個螢火蟲錯誤(這是ajax帖子):

沒有為此對象定義無參數構造函數

嘗試將請求中的信息轉換為RegisterModel時,默認模型綁定無法處理該對象。

選項:

如果您想要接受列表作為請求參數之類的內容- 到列表的模型綁定MVC 4可能會有答案。

好的,我在類型上犯了一個錯誤,甚至都不知道我是怎么做的。

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

這是我的疲倦所致的一個錯誤;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM