0

I'm running the app in this Microsoft Tutorial and when I try to enter a url in the input box of the app (for instance, http://msdn.com or http://blogs.msdn.com/adonet as shown in the tutorial) and click submit I get the following validation error:

The value 'http://msdn.com' is not valid for Url.

When I debug the application I notice that in the following code of the tutorial the ModelState.IsValid value is showing as 'false'. What is missing here and how can it be fixed?

public IActionResult Create(Blog blog)
        {
            if(ModelState.IsValid)
            {
                _context.Blogs.Add(blog);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(blog);
        }

Points to note:

  1. I'm using latest ASP.NET Core 1.0 and VS2015-Update 3 [released on June 27, 2016] on windows 8.1
  2. In the Creat.chtml view of the tutorial, <input asp-for="Url" class="form-control" /> was initially showing as readonly so I added type="text" attribute there and it became read/write
  3. My app is an exact copy of the tutorial (I did copy/past) except that instead of choosing 'No Authentication' I chose 'Individual User Accounts' option when creating the poroject. But that, I think, should not make any difference.

UPDATE

I added var errors = ModelState.Values.SelectMany(v => v.Errors); just above if(ModelState.IsValid) statement and I see the following in the debug window showing the values of error collection but can't figure out the cause of error. Maybe. someone can help:

UPDATE 2:

I found my mistake. I was using int datatype in public int Url { get; set; } property of the Blog class instead of using string. Changed it to string and it's working now. Someone may help what the error in image is saying.

enter image description here

nam
  • 21,967
  • 37
  • 158
  • 332
  • What does the error in the `ModelState` say? You can view all of the error details - here's how: http://stackoverflow.com/questions/1352948/how-to-get-all-errors-from-asp-net-mvc-modelstate – PeteGO Jul 03 '16 at 00:00
  • Do you have `@model EFGetStarted.AspNetCore.NewDb.Models.Blog` at the top? – prospector Jul 03 '16 at 00:19
  • @prospector Yes, I do have that on top and it does get recognized by intellisense as well. – nam Jul 03 '16 at 02:15
  • @prospector I resolved the issue. Please see UPDATE 2 in my post. – nam Jul 03 '16 at 04:12
  • Consider posting this is a self-answer so it plays nice with searches, etc. – jdphenix Jul 03 '16 at 04:14
  • @jdphenix Per your advice I just did it -explaining the details. Thank you. – nam Jul 03 '16 at 15:03

1 Answers1

1

I resolved the issue. There was a mistake on my part. I was using the datatype int in the property public int Url { get; set; } of the Blog class. Changed it to string. Then deleted the database in SQL Server and corresponding migration folder in the project. Re-ran the package manager commands Add-Migration MyFirstMigration -context BloggingContext and Update-Database -context BloggingContext. It's working now. Although the app was a copy/paste from this Microsoft Tutorial, later I tried to test creating a property through a short-cut key as explained here but forgot that the short-cut key creates the datatype as int. Thanks to all the readers who may have tried to help.

Community
  • 1
  • 1
nam
  • 21,967
  • 37
  • 158
  • 332