Asp.net mvc 为什么在ASP.NET MVC中出现此错误?
错误: 传递到字典的模型项的类型为“EventManagementSystem.Models.RegisterManagementModelViewModel”,但此字典需要类型为“EventManagementSystem.Models.RegisterViewModel”的模型项 控制器:Asp.net mvc 为什么在ASP.NET MVC中出现此错误?,asp.net-mvc,viewmodel,Asp.net Mvc,Viewmodel,错误: 传递到字典的模型项的类型为“EventManagementSystem.Models.RegisterManagementModelViewModel”,但此字典需要类型为“EventManagementSystem.Models.RegisterViewModel”的模型项 控制器: namespace EventManagmentSystem.Controllers { public class CustomerController : Controller {
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);
}
}
}
视图
RegisterManageModelViewModel.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>
}
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; }
}
}
检查控制器中寄存器应返回的视图。现在,它返回Register.cshtml视图和类型为registerManagementModelViewModel的模型。但是,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);
}
public ActionResult Register(RegisterPageModelViewModel rm)
{
...
RegisterViewModel model = new RegisterViewModel{...};
return View(model);
}
public ActionResult Register(RegisterPageModelViewModel rm)
{
...
return View("RegisterPageModelViewModel", rm);
}