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

问题描述 投票:0回答:1

我的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);
    }
  }


}

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

我想念什么吗?这没有道理。

UPDATE

查看

位于视图/用户中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>
c# asp.net-mvc entity-framework edmx
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.