4

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.

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.

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

Source Error:

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


        }
von v.
  • 16,868
  • 4
  • 60
  • 84
Anna Marie Rapa
  • 119
  • 1
  • 3
  • 13
  • 1
    You are posting `NewFixedAccountViewModel` and yet your view is bound to `NewFixedAccountViewModel`. You might want to look into that first. – von v. May 01 '13 at 12:08
  • He means your CreateNewFixedBankAccount action is accepting `NewAccountViewModel` and your view has a model declared as `NewFixedAccountViewModel`. – Paul Welbourne May 01 '13 at 12:11
  • You might need to check that all your Models parameters that you are passing back from the view on postback have both 'getters' and 'setters' to ensure the model can be bound back up. – Paul Welbourne May 01 '13 at 12:13
  • yes I rearranged that but it still giving me the error :/ – Anna Marie Rapa May 01 '13 at 12:33

2 Answers2

10

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

  • public NewFixedAccountViewModel(string username)
  • public NewFixedAccountViewModel(int accountID, string username)

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 
}

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

    }
adelb
  • 791
  • 7
  • 26
  • Do you store the logged in username in a session or another object? – adelb May 01 '13 at 14:59
  • When I did that nothing happens when the button is clicked -_- – Anna Marie Rapa May 01 '13 at 15:00
  • It doesn't even do a postback? If i understand correctly this constructor populates the select list properties right? And you need the username to retrieve the related accounts. I don't know how your authentication is but maybe you can take the username from a session or from User.Identity.Name – adelb May 01 '13 at 15:07
  • Do you know perhaps why the accountFrom getter and setter is doing an infinate loop and giving me the following error pls? An unhandled exception of type 'System.StackOverflowException' occurred in internetBankingApplication.dll – Anna Marie Rapa May 01 '13 at 15:59
  • Because you're assigning the value to the same property. So when you set a value to accountFrom, it goes into the setter. But inside the setter you do accountFrom = value again. Which again triggers the setter and so on. – adelb May 01 '13 at 16:04
  • You can just write public int accountFrom { get; set; } – adelb May 01 '13 at 16:10
  • No.. I need to get the value selected from the viewmodel – Anna Marie Rapa May 01 '13 at 16:27
  • The value will still be set using that method. It's just .net automatically manage how the value is stored. See http://stackoverflow.com/questions/61480/class-definition-using-get-set – adelb May 01 '13 at 16:33
  • but it is used to display to get the selected value from the drop downlist – Anna Marie Rapa May 01 '13 at 16:41
  • :) i FIGURED IT OUT! THX.. JUST FEW ERRORS LEft – Anna Marie Rapa May 01 '13 at 16:45
  • do you have any idea why Currencies = new SelectList( ManageCurrency.Instance.getCurrencies(), "id", "name", _Account.currency); is giving me this error? pls Object reference not set to an instance of an object. – Anna Marie Rapa May 01 '13 at 16:51
0

I'm not sure if this is quite right, but I think what's happening is you've declared constructors for your model. So it is trying to create a new object based on your model class, but can't because there isn't a constructor that accepts no parameters. I think what you need to do is create a new model in your controller with one of the constructors you have defined and pass it to the view:

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

Then it'll bind to the model object you have created with the appropriate constructor and bind the form fields to that instead instead of trying to create a new one.

ssn771
  • 1,270
  • 2
  • 17
  • 20