【问题标题】:MVC ASP.NET No parameterless constructor defined for this objectMVC ASP.NET 没有为此对象定义无参数构造函数
【发布时间】:2013-04-25 10:16:09
【问题描述】:

当我单击提交以创建帐户时,会显示以下错误。我找不到为什么它会给出以下错误。我也尝试过使用断点和调试,但没用

没有为此对象定义无参数构造函数。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.MissingMethodException:没有无参数 为此对象定义的构造函数。

来源错误:

在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

AccountController.cs

    [HandleError]
        public ActionResult CreateNewFixedBankAccount()
        {
            ViewBag.AccountFromList = new SelectList(
           ManageAccount.Instance.GetAccounts(), "accountID", "name");
            ViewBag.AccountTypes = new SelectList(ManageAccountType.Instance.GetNonFixedTypes(), "id", "type");
            ViewBag.Durations = new SelectList(ManageDuration.Instance.GetAllDurations(), "id", "duration1");


            ViewBag.Currencies = new SelectList(ManageCurrency.Instance.getCurrencies(), "id", "symbol");
            return View();


        }

        //
        // POST: /Account/CreateNewFixedBankAccount
        [HttpPost]
        [HandleError]
        public ActionResult CreateNewFixedBankAccount(ViewModel.NewAccountViewModel model, string username)
        {

            return View();
        }

___________________________________________________________________________________________

CreateNewFixedBankAccount.cshtml 下面的提交按钮给出了错误

    @model internetBankingApplication.ViewModel.NewFixedAccountViewModel    
    @{
        ViewBag.Title = "CreateNewFixedBankAccount";
    }


    @using (Html.BeginForm())
    {

        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/_Layout.cshtml";
        <fieldset>
            <legend>Fixed Account Details</legend>

            <div class="editor-label">
            <br />
     <b>@Html.LabelFor(model => model.Name)</b>
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
            </div>

            <div class="editor-label">
            <br />
                <b> @Html.LabelFor(model => model.UserName)</b>
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.UserName)
            </div>

            <div class="editor-label">
            <br />
             <b> @Html.Label("Account From")</b>
            </div>
            <div class="editor-field">
                 @Html.DropDownListFor(model => model.accountFrom, (SelectList)ViewBag.AccountFromList)
            </div>

            <div class="editor-label">
            <br />
               <b>  @Html.LabelFor(model => model.AvailableBalance)</b>
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.AvailableBalance)
            </div>


            <div class="editor-label">
            <br />
     <b>@Html.LabelFor(model => model.currency)</b>
            </div>
            <div class="editor-field">
                 @Html.DropDownListFor(model => model.currency, (SelectList)ViewBag.Currencies)
            </div>

            <div class="editor-label">
            <br />
               <b>  @Html.LabelFor(model => model.Description)</b>
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Description)
            </div>

            <div class="editor-label">
            <br />
                <b> @Html.LabelFor(model => model.DateOpened)</b>
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.DateOpened)
            </div>


            <div class="editor-label">
            <br />
             <b> @Html.Label("Account Duration")</b>
            </div>
            <div class="editor-field">
                 @Html.DropDownListFor(model => model.duration, (SelectList)ViewBag.Durations)
                 &nbsp;
                  @Html.ActionLink("View Duration Details","DurationDetails","Duration")
            </div>


            <div class="editor-label">
            <br />
             <b> @Html.Label("Account Renew")</b>
            </div>
            <div class="editor-field">
                 @Html.CheckBoxFor(model => model.renew)

            </div>


            <p>
                <input type="submit" value="CreateNewFixedBankAccount" /></p>
        </fieldset>
    }



___________________________________________________________________________________________

NewFixedAccountViewModel.cs

此视图模型将从数据库中的不同表中获取与帐户有关的详细信息。

    public class NewFixedAccountViewModel
    {
        private account _Account { get; set; }

        private fixedAccount _FixedAccount { get; set; }

        private int AccountFrom { get; set; }

        public SelectList AccountTypes { get; set; }

        public SelectList Durations { get; set; }

        public SelectList AccountFromList { get; set; }

        public SelectList Currencies { get; set; }

        public int ID
        {
            get
            {
                return _Account.accountID;
            }
        }

        [Required]
        [Display(Name = "Account Name")]
        public string Name
        {
            get
            {
                return _Account.name;
            }
            set
            {
                _Account.name = value;
            }
        }

        [Required]
        [Display(Name = "Account From")]
        public int accountFrom
        {
            get
            {
                return accountFrom;
            }
            set
            {
                accountFrom = value;
            }
        }

        public string AccountFromName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageAccount.Instance.GetAccountBYID(accountFrom).name;
                }
                catch { }
                return result;
            }
        }


        [Required]
        [Display(Name = "Account Type")]
        public int typeID
        {
            get
            {
                return _Account.typeID;
            }
            set
            {
                _Account.typeID = value;
            }
        }

        public string AccountTypeName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageAccountType.Instance.getTypesByID(_Account.typeID).type;
                }
                catch { }
                return result;
            }
        }


        [Required]
        [Display(Name = "Duration")]
        public int duration
        {
            get
            {
                return _FixedAccount.duration;
            }
            set
            {
                _FixedAccount.duration = value;
            }
        }

        public string durationName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageDuration.Instance.GetDurationById(_FixedAccount.duration).duration1;
                }
                catch { }
                return result;
            }
        }

        [Required]
        [Display(Name = "Available Balance")]
        public decimal AvailableBalance
        {
            get
            {
                return _Account.availableBalance;
            }
            set
            {
                _Account.availableBalance = value;
            }
        }

        [Required]
        [Display(Name = "Currency")]
        public int currency
        {
            get
            {
                return _Account.currency;
            }
            set
            {
                _Account.currency = value;
            }
        }

        public string CurrencyName
        {
            get
            {
                string result = string.Empty;
                try
                {
                    result = ManageCurrency.Instance.getTypesByID(_Account.currency).name;
                }
                catch { }
                return result;
            }
        }


        [Required]
        [Display(Name = "Account Description")]
        public string Description
        {
            get
            {
                return _Account.description;
            }
            set
            {
                _Account.description = value;
            }
        }


        [Required]
        [Display(Name = "Account Renew")]
        public bool renew
        {
            get
            {
                if (_FixedAccount.renew == 0)
                {
                    return false;
                }
                return true;
            }
            set
            {
                if (value == false)
                {
                    _FixedAccount.renew = 0;
                }
                else
                {

                    _FixedAccount.renew = 1;
                }
            }
        }


        [Required]
        [Display(Name = "Date Opened")]
        public DateTime DateOpened
        {
            get
            {
                return _Account.dateOpened;
            }
            set
            {
                _Account.dateOpened = value;
            }
        }

        [Required]
        [Display(Name = "Username")]
        public string UserName
        {
            get
            {
                return _Account.username;
            }
            set
            {
                _Account.username = value;
            }
        }

        public NewFixedAccountViewModel(string username)
        {
            AccountTypes = new SelectList(
            ManageAccountType.Instance.getTypes(), "id", "type", _Account.typeID);


            AccountFromList = new SelectList(
            ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);

            Currencies = new SelectList(
            ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency);

            Durations = new SelectList(
            ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);


        }

        public NewFixedAccountViewModel(int accountID, string username)
        {
            _Account = ManageAccount.Instance.GetAccountBYID(accountID);

            _FixedAccount = ManageFixedAccount.Instance.GetFixedAccountByID(accountID);

            AccountFromList = new SelectList(
            ManageAccount.Instance.GetUserAccounts(username), "accountID", "name", accountFrom);

            AccountTypes = new SelectList(
            ManageAccountType.Instance.getTypes(), "id", "type", _Account.accountType);

            Currencies = new SelectList(
            ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency);

            Durations = new SelectList(
            ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);


        }

【问题讨论】:

  • 您正在发布NewFixedAccountViewModel,但您的视图绑定到NewFixedAccountViewModel。你可能想先调查一下。
  • 他的意思是您的 CreateNewFixedBankAccount 操作正在接受 NewAccountViewModel 并且您的视图有一个声明为 NewFixedAccountViewModel 的模型。
  • 您可能需要检查您在回发时从视图传回的所有模型参数是否同时具有“getter”和“setter”,以确保可以重新绑定模型。
  • 是的,我重新安排了它,但它仍然给我错误:/

标签: asp.net-mvc asp.net-mvc-3 viewmodel asp.net-mvc-viewmodel


【解决方案1】:

这是因为您的 ViewModel 类中没有无参数构造函数。你有 2 个构造函数:

  • public NewFixedAccountViewModel(字符串用户名)
  • public NewFixedAccountViewModel(int accountID, string username)

当您提交表单时,在模型绑定期间,控制器正在尝试创建模型的新实例,但它没有找到无参数构造函数。

所以你需要:

public NewFixedAccountViewModel() { 
// Your code here 
}

你可以做的可能是使用用户名属性而不是用户名参数,所以你的代码将是:

public NewFixedAccountViewModel()
    {
        AccountTypes = new SelectList(
        ManageAccountType.Instance.getTypes(), "id", "type", _Account.typeID);


        AccountFromList = new SelectList(
        ManageAccount.Instance.GetUserAccounts(Username), "accountID", "name", accountFrom);

        Currencies = new SelectList(
        ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency);

        Durations = new SelectList(
        ManageDuration.Instance.GetAllDurations(), "id", "duration", _FixedAccount.duration);

    }

【讨论】:

  • 您将登录的用户名存储在会话中还是其他对象中?
  • 当我这样做时,单击按钮时没有任何反应-_-
  • 它甚至不做回发?如果我理解正确,这个构造函数会填充选择列表属性吗?您需要用户名来检索相关帐户。我不知道您的身份验证如何,但也许您可以从会话或 User.Identity.Name 中获取用户名
  • 您知道为什么 accountFrom getter 和 setter 会执行无限循环并给我以下错误吗? internetBankingApplication.dll 中出现“System.StackOverflowException”类型的未处理异常
  • 因为您将值分配给同一个属性。因此,当您为 accountFrom 设置一个值时,它会进入 setter。但是在设置器中,您再次执行 accountFrom = value。这再次触发了 setter 等等。
【解决方案2】:

我不确定这是否完全正确,但我认为您已经为模型声明了构造函数。所以它试图根据您的模型类创建一个新对象,但不能,因为没有不接受任何参数的构造函数。我认为您需要做的是使用您定义的构造函数之一在控制器中创建一个新模型并将其传递给视图:

NewFixedAccountViewModel model = new NewFixedAccountViewModel(username);
return View(model);

然后它将绑定到您使用适当的构造函数创建的模型对象,并将表单字段绑定到该模型对象,而不是尝试创建新的。

【讨论】:

  • 现在它在选择列表对象引用中给了我另一个错误,未设置为对象的实例。
猜你喜欢
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 2012-08-31
  • 2021-12-21
相关资源
最近更新 更多