数据库.[table].Add(obj)无法转换.错误代码CS1503



我的UserController.cs文件中的ASP.Net MVC 5应用程序出现错误。我得到的错误是CS1503,但与其他问题/解决方案相比,原因没有意义。

完整错误为:

CS1503参数1:无法从第25行的"Elevate_Your_Pitch.Models.tblRegistration"转换为"Elevate_Your_Pitch.Models.user">

我的UserController.cs(控制器(、tblRegistration.cs(型号(和Registration.Context.cs(edmx(文件的代码如下:

UserController.cs

using System;
using Stystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.MVC;
using Elevate_Your_Pitch.Models;
namespace Elevate_Your_Pitch.Controllers
{
public class UserController : Controller
{
// Get: User
public ActionResult Index() { return View(); }
[HttpPost]
public ActionResult Index(tblRegistration obj)
{
if (ModelState.IsValid)
{
elevate_your_pitchEntities db = new elevate_your_pitchEntities();
db.users.Add(obj); //This is line 25 and (obj) is causing the error
db.SaveChanges();
}
return View(obj);
}
}

}

注册.Context.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Elevate_Your_Pitch.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class elevatey_your_pitchEntities : DbContext
{
public elevatey_your_pitchEntities() : base("name=elevatey_your_pitchEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<user> users { get; set; }
}
}

tbl注册.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Elevate_Your_Pitch.Models
{
[Table("user")]
public partial class tblRegistration
{
[Key]
public int UserID { get; set;  }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailID { get; set; }
public DateTime DateOfBirth { get; set; }
public string Password { get; set; }
public string IsEmailVerified { get; set; }
}
}

我是不是错过了什么?这没有道理。

更新

查看

位于视图/用户中Index.cshtml

@model Elevate_Your_Pitch.Models.tblRegistration
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>tblRegistration</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateOfBirth, "", 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.IsEmailVerified, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.IsEmailVerified, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.IsEmailVerified, "", 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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>

对我来说,我解决了这个错误,我找到了两个同名的类Registration类我更改了一个并保留了一个类名Registration及其工作。

相关内容

最新更新