2016-11-13 4 views
-1

오류 : 사전에 전달왜 ASP.NET MVC에서이 오류가 발생합니까?

모델 항목 유형 'EventManagmentSystem.Models.RegisterPageModelViewModel'의이지만,이 사전은 유형의 모델 항목을 요구한다 'EventManagmentSystem.Models.RegisterViewModel'.

컨트롤러 :

namespace EventManagmentSystem.Controllers 
{ 
    public class CustomerController : Controller 
    { 
     // GET: Customer 
     public ActionResult Index() 
     { 
      return View(); 
     } 
     public ActionResult Register(RegisterPageModelViewModel rm) 
     { 
      if(ModelState.IsValid) 
      { 

       RegistrationPageModel rp = new RegistrationPageModel 
       { 
        C_Name = rm.C_Name, 
        C_Password = rm.C_Password 
       }; 
      } 

      return View(rm); 
     } 
    } 
} 

조회수

RegisterPageModelViewModel.cshtml :

@model EventManagmentSystem.Models.RegisterPageModelViewModel 

@using (Html.BeginForm("Register", "Customer",FormMethod.Post, new { @class = "form-inline", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>RegisterPageModelViewModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.C_Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.C_Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.C_Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.C_Password, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.C_Password, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.C_Password, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

Register.cshtml :

@model EventManagmentSystem.Models.RegisterViewModel 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>RegisterViewModel</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

모델

RegisterPageModelViewModel.cs :

namespace EventManagmentSystem.Models 
{ 
    public class RegisterPageModelViewModel 
    { 
     [Required(ErrorMessage = "Name is Required")] 
     public string C_Name { get; set; } 

     [Required(ErrorMessage = "Password is Required")] 
     public string C_Password { get; set; } 
    } 
} 

RegistrationPageModel.cs :보기 컨트롤러에 등록에 의해 반환해야하는지

namespace EventManagmentSystem.Models 
{ 
    public class RegistrationPageModel 
    { 
     public int C_ID { get; set; } 
     public string C_Name { get; set; } 
     public string C_Password { get; set; } 
     public string C_Gender { get; set; } 
    } 
} 

답변

0

확인합니다. 지금 그것은 Register.cshtml 유형 및 유형 RegisterPageModelViewModel를 가진 모형을보고 돌려 보낸다. 그러나 Register.cshtml에는 RegisterViewModel 유형의 모델이 필요하며이 오류가 발생합니다.

필요한 작업에 따라 RegisterViewModel을 사용하여 모델 유형을 변경하십시오. 같이있을 수 :

public ActionResult Register(RegisterPageModelViewModel rm) 
{   
    ... 
    RegisterViewModel model = new RegisterViewModel{...}; 
    return View(model); 
} 

또는보기를 변경하려면 다음과 같이 할 수 있습니다 :

public ActionResult Register(RegisterPageModelViewModel rm) 
{   
    ... 
    return View("RegisterPageModelViewModel", rm); 
}