db. [таблица] .Add (obj) не может конвертировать. Код ошибки CS1503 - PullRequest
0 голосов
/ 03 ноября 2018

Я получаю сообщение об ошибке в моем приложении ASP.Net MVC 5 в моем файле UserController.cs. Я получаю ошибку CS1503, но причина не имеет смысла по сравнению с другими проблемами / решениями.

Полная ошибка:

CS1503 Аргумент 1: невозможно преобразовать из 'Elevate_Your_Pitch.Models.tblRegistration' в Elevate_Your_Pitch.Models.user 'в строке 25

У меня будет код для моих файлов 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

View

Расположен в Представлениях / Пользователь 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>
...