0

I'm trying to Insert my product properties into SQL table, I have a razor page that gets input data that needs to be inserted but i don't know how to insert them This is my product model:

namespace Market.ViewModels
{
    public class ProductListView
    {
        public Guid ID { get; set;  }
        public string ProductName { get; set;  }
        public decimal ProductPrice { get; set;  }

        
    }
}

This is my razor page :

enter image description here

2
  • Which steps did you take so far to get Entity Framework operational? Commented Oct 24, 2021 at 9:10
  • 1
    Learn the tutorial Commented Oct 24, 2021 at 12:51

1 Answer 1

1

I made a simple example, using EF Core code first to create a database and then query the data, the process of binding the value to the page is as follows.

1.The first dependency packages used are as follows:

enter image description here

Model:

 public class ProductListView
    {

        [Key]
        public Guid ID { get; set; }
        public string ProductName { get; set; }
        public double ProductPrice { get; set; }
    }

I modified your ProductPrice type, because there will be problems with this type during migration. If you must change the type, refer to this article:

http://jameschambers.com/2019/06/No-Type-Was-Specified-for-the-Decimal-Column/

Create Model and Context Classes:

Now you can add the database context : name the class TestDbContext and click Add and change the code in TestDbContext.cs as follows:

 public class TestDbContext:DbContext
    {
        public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
        {
                
        }

        public DbSet<ProductListView> productListViews { get; set; }
    }

Connection string you need to write inside the appsetting.json file as follows:

 "ConnectionStrings": {
    "DefaultDatabase": "Your DB"
  }

In ASP.NET Core, services such as the DB context must be registered with the dependency injection container. The container provides the service to controllers. Update Startup.cs with the following highlighted code:

   public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
                services.AddDbContext<TestDbContext>(item => item.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));
            }

In order to create the migration code, we use the "add-migration MigrationName" command. After the add migration command is successfully executed, it will create a folder named "Migration" in the project. We only created the migration responsible for creating the database and its tables. script. But we have not yet created the actual database and tables. So let's execute the migration script and generate the database and tables. Therefore, to execute the migration script we must execute the'update-database' command.

Next, let us create a context class, define the database connection and register the context. Then perform the query work in the controller, and then return the data.

 public class TestController : Controller
    {

        private readonly TestDbContext _context;

        public TestController(TestDbContext context)
        {
            _context = context;
        }
        public IActionResult Test(ProductListView product)
        {
            var value = _context.productListViews.SingleOrDefault(item => item.ProductPrice == 12.1);

            product.ProductName = value.ProductName;
            product.ProductPrice = value.ProductPrice;
           
            return View(product);
        }
    }

View:

@model WebApplication132.Models.ProductListView
<h1>AddProducts</h1>

<div class="row">
    <div class="col-md-12">
        <form method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="ProductName"></label>
                <input asp-for="ProductName" class="form-control" />
            </div>

            <div class="form-group">
                <label asp-for="ProductPrice"></label>
                <input asp-for="ProductPrice" class="form-control" />
            </div>

            <button type="Add Product" class="btn-primary">Add Product</button>
        </form>
    </div>
</div>

Db data:

enter image description here

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.