3

제출을 클릭하여 계정을 만들면 다음 오류가 표시됩니다. 왜 다음과 같은 오류가 발생하는지 찾을 수 없습니다. 또한 내가 중단 점 및 디버깅을 사용하려고하지만 CreateNewFixedBankAccount.cshtml 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(); 
     } 

___________________________________________________________________________________________ 

쓸모가 없었습니다 제출 버튼 아래의 오류

@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'에 묶여 있습니다. 먼저 살펴 보길 원할 것입니다. –

+0

그는 당신의 CreateNewFixedBankAccount 액션이'NewAccountViewModel'을 수락하고 당신의 뷰가'NewFixedAccountViewModel'로 선언 된 모델을 가지고 있음을 의미합니다. – wellers

+0

포스트 백 뷰에서 다시 전달할 모든 모델 매개 변수가 모델을 다시 바인딩 할 수 있도록 'getters'와 'setters'를 모두 갖고 있는지 확인해야합니다. – wellers

답변

9

ViewModel 클래스에 매개 변수없는 생성자가 없기 때문입니다. 당신이이 생성자를 가지고 :

  • NewFixedAccountViewModel (캐릭터 이름)
  • 공공 NewFixedAccountViewModel (INT ACCOUNTID에, 문자열 사용자 이름)

당신이 결합 모델 중, 양식을 제출 , 컨트롤러가 노력하고 공개 모델의 새 인스턴스를 만들지 만 매개 변수없는 생성자는 찾지 않습니다.

그래서 당신이 필요합니다 :

public NewFixedAccountViewModel() { 
// Your code here 
} 

당신은 당신의 코드가 될 것입니다, 그래서 어쩌면, 대신 사용자 이름 매개 변수의 이름 속성을 사용한다 할 수있는 일 : 당신은`NewFixedAccountViewModel` 게시하는

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

로그인 한 사용자 이름을 세션 또는 다른 객체에 저장합니까? –

+0

버튼을 클릭했을 때 아무 일도 일어나지 않았습니다. -0- –

+0

다시 게시하지 않습니다? 내가 올바르게 이해한다면이 생성자는 선택 목록 속성을 올바르게 채 웁니까? 그리고 관련 계정을 검색하려면 사용자 이름이 필요합니다. 나는 당신의 인증이 어떨지 모르지만 세션이나 User.Identity.Name에서 사용자 이름을 가져올 수 있습니다. –

0

이게 옳은 것인지 확실치 않지만, 어떤 일이 일어나고 있는지는 모델에 대한 생성자가 선언 된 것입니다. 따라서 모델 클래스를 기반으로 새 객체를 만들려고하지만 매개 변수를 허용하지 않는 생성자가 없으므로이를 수행 할 수 없습니다.

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

그런 다음 당신이 함께 만든 모델 객체에 바인딩 할 수 있습니다 : 난 당신이 정의 된 생성자 중 하나와 컨트롤러의 새로운 모델을 생성하고 뷰에 전달되어 무엇을해야하는지 생각 적절한 생성자를 만들고 폼 필드를 새로운 필드를 만드는 대신 폼 필드를 바인딩합니다.

+0

이제 선택 목록에 다른 오류가 표시됩니다. 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. –

관련 문제