MVC ASP.NET没有为此对象定义无参数构造函数 [英] MVC ASP.NET No parameterless constructor defined for this object

查看:93
本文介绍了MVC ASP.NET没有为此对象定义无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I click submit in order to create an account the following error is displayed. I can't find why it is giving the following error. Also I tried to use breakpoints and debugging but it was useless

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

No parameterless constructor defined for this object.

描述:执行以下操作时发生未处理的异常 当前的Web请求.请查看堆栈跟踪以获取更多信息 有关错误及其在代码中起源的信息.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

源错误:

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

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

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 下面的提交按钮给出了错误

CreateNewFixedBankAccount.cshtml The submit button below gives the error

    @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

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

This view model will get details that have to do with the account from different tables in the database.

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


        }

推荐答案

这是因为ViewModel类中没有无参数的构造函数.那里有2个构造函数:

it's because you don't have parameterless constructor in your ViewModel class. You have 2 constructors there:

  • 公共NewFixedAccountViewModel(字符串用户名)
  • 公共NewFixedAccountViewModel(int帐户ID,字符串用户名)

提交表单时,在模型绑定期间,控制器将尝试创建模型的新实例,但找不到无参数的构造函数.

When you submit the form, during model binding, the controller is trying to create a new instance of the model, but it doesn't find parameterless constructor.

因此,您需要具备以下条件:

So you need to have:

public NewFixedAccountViewModel() { 
// Your code here 
}

您可以做的是使用Username属性而不是username参数,因此您的代码将是:

What you can do is maybe use the Username property instead of username parameter, so your code will be:

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

    }

这篇关于MVC ASP.NET没有为此对象定义无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆