0

This questions seems to have been answered before, but those answers don't work in my case as far as I know, since the codes in the previous questions were much different than my code.

I'm very new at MVC by the way.

This is the error I get:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleMVC.Models;

namespace SimpleMVC.Controllers
{
    public class AdminController : Controller
    {
        // GET: Admin
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddMovie(string Title, int Price)
        {
            using (MovieDb_Entities db = new MovieDb_Entities()){
                Movies item = new Movies();
                item.Title = Title;
                item.Price = Price;

                db.Movies.Add(item);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Hey()
        {
            return View();
        }
    }
}

Partial view:

<p>Add new movie</p>
<form method="post" action="/Admin/AddMovie">
    <input type="text" name="Title" placeholder="Title" /><br />
    <input type="text" name="Price" placeholder="Price" /><br />
    <input type="submit" value="Add Movie" />
</form>

Thank you in advance.

Mikkel
  • 1,853
  • 1
  • 15
  • 31
  • *See 'EntityValidationErrors' property for more details.* - did you do this? The first step is to follow the debugging instructions in the error message. – Ant P Aug 26 '14 at 14:20
  • Add the model please... - the validation is defined there – yossico Aug 26 '14 at 14:20
  • @AntP - I would if I knew where that property is, I don't know where to read it. – Mikkel Aug 26 '14 at 14:29
  • @yossico - How would I send you the model? As I said, I'm a huge MVC noob, but I'm trying to learn it – Mikkel Aug 26 '14 at 14:30
  • @Mikkel It's a property of the exception itself. I would advise debugging your application, stepping through to ensure all the values are set as you expect and then inspecting the exception when it is thrown. There are lots of resources floating around on debugging in Visual Studio. – Ant P Aug 26 '14 at 14:31
  • Thanks for your assistance both of you, it appears that after debugging my application, it automatically built it and it worked after. So I must have forgotten to build my application, which I thought I did tons of times. I'm sorry for taking your time, and once again, thank you for your time and help! – Mikkel Aug 26 '14 at 14:36
  • You should wrap `string Title, int Price` into a Model that has server side validation for you. As it is a client will generate an exception if they submit with a form were either field is blank (`null`) or the wrong type (trying to submit a `akjd` for `Price`). Inside a model you can handle that though and return meaningful error messages to the end user. – siva.k Aug 26 '14 at 14:41
  • @siva.k - Thank you, I'll make sure I do that in the future – Mikkel Aug 26 '14 at 15:09
  • If you use **Entity Framework** you can have a look at my answer on [Solution for “Validation failed for one or more entities. See 'EntityValidationErrors' property for more details](http://stackoverflow.com/questions/21486072/solution-for-validation-failed-for-one-or-more-entities-see-entityvalidatione/29031857#29031857). Hope this helps... – Murat Yıldız Jan 26 '16 at 23:13

1 Answers1

0

Building the application fixed it. This is something I forgot to do.

Sorry.

Mikkel
  • 1,853
  • 1
  • 15
  • 31