为什么我的表仅用ID填充空对象?

时间:2019-11-26 07:23:41

标签: c# html asp.net-mvc razor

我的电影和客户之间有很多关系。但是每次我创建一个新客户并从下拉菜单中为该客户选择电影时,都会创建新客户,但电影版位为空,然后当我在数据库中检查我的电影表时,新电影将以空值创建来自新的电影ID。而我的CustomerMovie表(许多关系表)将具有针对新电影ID的新客户ID。为什么要通过吸引新客户来制作带有空值的新电影,并且不会将现有电影添加到客户中。

另外请注意,我对Razor @Html标签不是很熟悉,所以我只使用了普通的For循环,至少在现在对我来说更有意义。

这是我的客户模型

 public class Customer
    {
        public Customer()
        {
            this.Movies = new HashSet<Movie>();
        }
        public int id { get; set; }
        [StringLength(255)]
        public String Name { get; set; }
        public bool IsSubscribedToNewsletter { get; set; }
        public int CurrentMembershipTypeId { get; set; }
        public virtual MembershipType CurrentMembershipType { get; set;}
        public virtual ICollection<Movie> Movies { get; set; }
    }

这是我的电影模特

    public class Movie
    {
        public Movie()
        {
            this.Customers = new HashSet<Customer>();
        }
        public int Id { get; set; }
        //[Required(ErrorMessage = "Name must be specified")]
        public string Name { get; set; }
        public virtual ICollection<Customer> Customers { get; set; }
    }
}

这是我的上下文类:

public class CustomerContext : DbContext
{

    public CustomerContext() : base("CustomerContext")
    {
    }

    public DbSet<MembershipType> MembershipTypes { get; set; }
    public DbSet<Customer> Customers { get; set; }

    public DbSet<Movie> Movies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Customer>()
                    .HasMany<Movie>(s => s.Movies)
                    .WithMany(c => c.Customers)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("CustomerId");
                        cs.MapRightKey("MovieId");
                        cs.ToTable("CustomersMovies");
                    });

        modelBuilder.Entity<Customer>()
       .HasRequired<MembershipType>(s => s.CurrentMembershipType)
       .WithMany(g => g.Customers)
       .HasForeignKey<int>(s => s.CurrentMembershipTypeId);

    }
}

这是客户控制者:

  public class CustomerController : Controller
    {
        // private ICustomerRepository iCustomerRepository;
        ICustomerRepository iCustomerRepository = new CustomerRepository(new CustomerContext());
        IMembershipTypeRepository iMembershipTypeRepository = new MembershipTypeRepository(new CustomerContext());
        IMovieRepository imovieRepository = new MovieRepository(new CustomerContext());
        CustomerContext CustomerContext = new CustomerContext();

        // GET: Customers
        public ActionResult Customer()
        {
            var customers = CustomerContext.Customers.Include(c => c.Movies);
            var Customers = customers.ToList();
            return View(Customers);
        }

        //public ActionResult CustomerForm(Customer Customer)
        public ActionResult CustomerForm(Customer customer)
        {
            var membershipTypes = iMembershipTypeRepository.GetMembershipTypes();
            var Movie = imovieRepository.GetMovies();
            ViewBag.Message = "Customers form is going to display: ";
            CustomerViewModel customerViewModel = new CustomerViewModel
            {
                MembershipTypes = CustomerContext.MembershipTypes,
                Movies = CustomerContext.Movies,
            };
            if (customer.IsSubscribedToNewsletter != true)
                ViewBag.Addon = "Selected";
            else
                ViewBag.Addon = "Not Selected";
            return View(customerViewModel);
        }

        public ActionResult Edit(int id)
        {
            Customer Customer = new Customer();
            Customer = iCustomerRepository.GetCustomerByID(id);
            CustomerViewModel customer = new CustomerViewModel
            {
                MembershipTypes = CustomerContext.MembershipTypes,
                Movies = CustomerContext.Movies,
                Customer = Customer
            };
            return View(customer);
        }

        [HttpPost]

        public ActionResult CustomerForm(int CustomerId, string CustomerName, bool CustomerisSubscribedToNewsLetter, int membershipTypeId, Movie movie)

        {

            Customer customer = new Customer();
           // Movie Movie = new Movie();

            customer.id = CustomerId;

            customer.Name = CustomerName;

            customer.IsSubscribedToNewsletter = CustomerisSubscribedToNewsLetter;
            customer.CurrentMembershipTypeId = membershipTypeId;

            customer.Movies.Add(movie);
            movie.Customers.Add(customer);


            iCustomerRepository.InsertCustomer(customer);
           // imovieRepository.InsertMovie(movie);

            iCustomerRepository.Save();
           // imovieRepository.Save();

            return RedirectToAction("Customer");

        }
        [HttpPost]
        public ActionResult Edit(int CustomerId, string CustomerName, bool CustomerIsSubscribedToNewsLetter, int membershipTypeId, IList<Movie> movie)
        {
            Customer Customer = new Customer();
            Customer.id = CustomerId;
            Customer.Name = CustomerName;
            Customer.IsSubscribedToNewsletter = CustomerIsSubscribedToNewsLetter;
            Customer.CurrentMembershipTypeId = membershipTypeId;
            Customer.Movies = movie;
            iCustomerRepository.UpdateCustomer(Customer);
            iCustomerRepository.Save();
            //CustomerContext.Movies.AddRange(movie);
            imovieRepository.Save();
            CustomerContext.SaveChanges();
            return RedirectToAction("Customer");
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            Customer customer = new Customer();
            customer = iCustomerRepository.GetCustomerByID(id);
            iCustomerRepository.DeleteCustomer(customer.id);
            iCustomerRepository.Save();
            return RedirectToAction("Customer");
        }

    }

这是我的电影控制器,目前仅列出所有功能:

 public class MovieController : Controller
    {
        IMovieRepository iMovieRepository = new MovieRepository(new CustomerContext());
        ICustomerRepository iCustomerRepository = new CustomerRepository(new CustomerContext());
        CustomerContext CustomerContext = new CustomerContext();

        public ActionResult Movie()
        {
            var Movies = iMovieRepository.GetMovies();
            //var movies = CustomerContext.Movies.Include(s => s.Customers);
            return View(Movies);
        }
    }

这是客户窗体视图:

@model WebApplication2.ViewModel.CustomerViewModel
@*<h4>
    <b>ID:</b> @ViewBag.id <br />
    <b>Name:</b> @ViewBag.Name <br />
    <b>Addon:</b> @ViewBag.Addon <br />
</h4>

<h2>CustomerForm</h2>

<form action="/Customer/CustomerForm" method="post">
    <table>
        <tr>
            <td>Id:</td>
            <td><input id="CustomerId" type="text" name="CustomerId" /></td>
        </tr>
        <tr>
            <td>Name:</td>
            <td><input id="CustomerName" type="text" name="CustomerName" /></td>
        </tr>
        <tr>
            <td>IsSubscribedToNewLetter:</td>
            <td><input id="CustomerisSubscribedToNewsLetter" type="text" name="CustomerisSubscribedToNewsLetter" /></td>
        </tr>
        <tr>
            <td>MembershipType:</td>
            <td>
                <select id="MembershipTypeId" name="MembershipTypeId">
                    @foreach (var membershipType in @Model.MembershipTypes)
                    {
                        <option value=@membershipType.Id>
                            @*@membershipType.SignupFee*@
                            @membershipType.Id
                        </option>
                    }
                </select>
            </td>
        </tr> 
        <tr>
            <td>Movie:</td>
            <td>
                <select id="MovieId" name="MovieId">
                    @*@Html.ListBoxFor(model => model.MovieId, Model.AllMovies, new { size = 8 })*@
                    @foreach (var movie in Model.Movies)
        {
            <option value=@movie.Id>
                @movie.Name
            </option>
        }
                </select>
            </td>
        </tr>

    </table>
    <input type="submit" value="save"/>
</form>

以下是我的客户视图:

@model  IEnumerable<WebApplication2.Models.Customer>

@{
    ViewBag.Title = "Customers";
}

<p>
    @Html.ActionLink("Create New", "CustomerForm")
</p>

<table>
    <tr>
        <th></th>
        <th>Number</th>
        <th>Name</th>
        <th>Subscription</th>
        <th>MembershipType signup fee</th>
        <th>Movies</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Name }) |
                @*@Html.ActionLink("Delete", "Delete", new { id = item.id }, new { onclick = "return confirm('Are you sure you wish to delete this article?');" })*@
                @*<p><a href="/Customer/Edit/@item.id">Edit Customer</a></p>*@
                <form method="post" action="/Customer/delete/@item.id">

                    <input type="submit" value="delete" />
                </form>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsSubscribedToNewsletter)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CurrentMembershipType.SignupFee)
            </td>
            <td>
                @foreach (var movie in item.Movies)
                 {
                    @movie.Name
                 }
            </td>
        </tr>
    }
</table>

0 个答案:

没有答案