ASP.net MVC 6 Cannot use New Scaffloded item

NARAWICH YOUBUA 1 Reputation point
2022-04-17T16:32:06.51+00:00

I am new with asp.net and C#. I tried to create my MVC webapp but when try to add New New Scaffloded item it popup an error

"There was an error running the selected code generator: 'startIndex cannot be larger than length of string. (Parameter 'startIndex')' "

My Dependencies version:

Microsoft.EntityFrameworkCore.Design 6.0.4
Microsoft.EntityFrameworkCore.SqlServer 6.0.4
Microsoft.EntityFrameworkCore.Tools 6.0.4
Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.3
Swashbuckle.AspNetCore 6.3.0

My Model

using System.ComponentModel.DataAnnotations;
namespace ThaiCul.Models
{
public class User
{

    [Required]
    public int ID { get; set; }

    [Required]
    public string? Username { get; set; }

    [Required]
    public string? Password { get; set; }

    [Required]
    public string? WriterName { get; set; }

    [Required]
    public string? Is_Admin { get; set; }

    [Required]
    public string? Is_Ban { get; set; }
}

}

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-04-18T07:26:23.593+00:00

    Hi @NARAWICH YOUBUA ,

    "There was an error running the selected code generator: 'startIndex cannot be larger than length of string. (Parameter 'startIndex')' "

    For this error, I checked some similar threads, that error comes from something like Substring, when the value passed into the startIndex param is greater than the total length of the string. For example, if you had a string like "Just Testing" and you did .Substring(12, 1), you'd get that exception, as the string is only 12 characters long (remember that the index is from 0, not 1, so the last index is 11).

    I have created a new MVC application and refer the tutorial and test your code, the New Scaffloded Item function works well. The result like this:

    193811-image.png

    You can refer the following steps to create a new MVC application and test.

    1. Create a new MVC application using the template, and select the Individual Accounts authentication type: 193770-image.png
    2. In the Models or Data folder, add the User class, and add the DbSet in the ApplicationDbContext class:
      public class ApplicationDbContext : IdentityDbContext  
      {  
          public DbSet<User> Users { get; set; }  
          public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
              : base(options)  
          {  
          }  
      }  
      
    3. In the Package Manager Console, use the following command to enable migration and generate the database:
      Add-Migration InitialCreate  
      Update-Database  
      
    4. In Solution Explorer, right-click the Controllers folder and select Add > New Scaffolded Item.
      In the Add Scaffold dialog, select MVC Controller with views, using Entity Framework > Add.
      Then, in the popup dialog, select the User class, ApplicationDbContext and click the Add button to add the controller and views. You could refer the document.

    I use the above steps, and it works well on my side, you can check it.

    If still not working, can you tell us your VS version, and can you create a simple sample to reproduce the problem and share it via github or Onedrive, then, we can help to check it.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


  2. panupong sudjaidee 1 Reputation point
    2022-09-26T05:29:45.27+00:00

    Hi i have a same problem with this post do you slove it?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.