3

当我单击提交以创建帐户时,将显示以下错误。我无法找到它为什么会给出以下错误。我也尝试过使用断点和调试,但没用MVC ASP.NET没有为此对象定义的无参数构造函数

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

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



___________________________________________________________________________________________ 

NewFix edAccountViewModel.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); 


     } 
+1

您发布'NewFixedAccountViewModel'但你的视图绑定到'NewFixedAccountViewModel'。你可能想先看看。 – 2013-05-01 12:08:44

+0

他意味着您的CreateNewFixedBankAccount操作正在接受'NewAccountViewModel',并且您的视图有一个声明为'NewFixedAccountViewModel'的模型。 – wellers 2013-05-01 12:11:34

+0

您可能需要检查您在回发视图中传回的所有模型参数都有'getters'和'setters',以确保模型可以重新绑定。 – wellers 2013-05-01 12:13:13

回答

9

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

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

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

所以你需要有:

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

    } 
+0

您是否将登录的用户名存储在会话或其他对象中? – 2013-05-01 14:59:53

+0

当我这样做时,点击按钮时什么也没有发生--_- – 2013-05-01 15:00:24

+0

它甚至不做回发?如果我理解正确这个构造函数填充选择列表属性权利?你需要用户名来检索相关账户。我不知道你的身份验证如何,但也许你可以从会话或从User.Identity.Name – 2013-05-01 15:07:09

0

我不确定这是否正确,但我认为发生了什么是您已经为您的模型声明构造函数。所以它试图创建一个基于你的模型类的新对象,但不能,因为没有一个不接受任何参数的构造函数。我认为你需要做的是建立在你的控制器的新模式与已定义的构造函数之一,它传递给视图什么:

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

然后,它会结合您与创建的模型对象相应的构造函数并将表单域绑定到该域,而不是尝试创建一个新域。

+0

现在它给我在选择列表中的另一个错误 对象引用未设置为对象的实例。 – 2013-05-01 15:25:29