db。[table] .Add(obj)无法转换。错误代码CS1503

时间:2018-11-03 02:08:26

标签: c# asp.net-mvc entity-framework edmx

我的UserController.cs文件中的ASP.Net MVC 5 App出现错误。我得到的错误是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);
    }
  }


}

Registration.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; }
  }
}

tblRegistrations.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>

1 个答案:

答案 0 :(得分:0)

对我来说,我解决了此错误,我发现2个同名类的注册类,我更改了一个,并保留了一个类名Registration及其工作。