Share the table schema, entities, and/or fluent source code.
Aliyilmaz
How is post table structure. I explained the problem enough
You've explained the problem but have not shared enough information for the community to provide an accurate solution. The code you shared....
var pcat = port.TProjeCategory.AsQueryable().ToList();
ViewBag.Pcat = pcat;
var proje = port.TProjects.AsQueryable().ToList();
ViewBag.Proje = proje;
...queries two tables. The code does not indicate if the tables are related in any meaningful way. The fact that you think you've provided enough code indicates you need to spend more time learning the fundamentals.
Products and Categories is a standard learning pattern. Usually there is a third table ProductCategory that contains a ProductId and CategoryId. A basic LINQ to Entities query can produce the products in a category. This concept is covered in just about
every get started EF Core tutorial.
If you want help from the community then we need to see your table schema otherwise we are just guessing what bugs exist in your design and code. Another option is setting aside time to learn LINQ to Entities fundamentals,
But I am using Entity DB First. So I shared DB Diagram, and code. I want to do explained.
We cannnot see the image as explained by Kathy. You'll need to publish the image on the internet and link to the image.
Second, this is an EF Core support forum and EF Core does not have the DB first feature only Code First. Are you in the wrong forum?
It's very puzzling why you are unable to explain the Product and Category design. Are you following a tutorial or downloaded the database? If so, point us to the tutorial. If you designed the tables yourself, which is how DB first works, then you should
be fully aware of how you designed the schema and how the IDs are related.
But, you still have not provided the EF version you are using. The example above is from EF 6 because, as stated above, EF Core does not have a DB first. I assume you posted in the wrong forum.
Lastly, I strongly recommend that you go through the Getting Tutorials found on this site. The code shown above is a fundamental concept that you need to understand before moving forward.
A Project can only have one Category. It can't have multiple Categories. It looks to me that all you need is a CategoryID column on the Project table definition.<br>
<br>
Just what are you trying to accomplish?
Are you using EF Core?
If you find the post has answered your issue, then please mark post as 'answered'.
So, your actual question is how to use a 3rd party library name mix it up? At the same time, it seems you do not understand EF and MVC fundamentals which is causing the confusion with the JavaScript Library. Build a View Model to shape the data for use
with mix it up. Read the mix it up documentation so you understand how to shape the View Model.
In the future, please be more explicit about the problem you are trying to solve. This allows the community to provide accurate assistance without having to ask several several clarifying followup questions.
IMO, you need a dropdownlist for Category that is part of the Project view for Project creation or edit. To create the Project, a Category must be selected with the dropdownlist holding the CategoryID that must be saved on the Project record.
Then if you want to have view that allows to select all Projects belonging to a particular Category selected from the dropdownlist, you can take the selected CategoryID and query the Project table for all Project records the have the CategoryID in the Project
record
The key I think is that you need a CategoryID on the Project table
If you find the post has answered your issue, then please mark post as 'answered'.
I did like your example. But I want to do with jquery. I dont want page is postback. How can I do ?
You change the requirements with each new post. At this point you have not shared a single line of jQuery code. I think you should try writing the jQuery/AJAX code yourself. Share your code if you run into trouble. Also, I think you are trying to use
a 3rd party library and need to read the documentation.
There's no response because your code does not submit the Id. If you were using debug tools like Dev Tools you should see errors. Also if you set a break point in VS you'll notice the action is not hit.
IMHO, your filter design is a little confusing too. Anyway, below is a basic example that builds my previous posts.
public class HomeController : Controller
{
private readonly TestDbEntities context;
public HomeController()
{
context = new TestDbEntities();
}
[HttpGet]
public ActionResult Index()
{
var results = context.T_Projects;
return View(results);
}
[HttpPost]
public JsonResult Search(string Category)
{
//Projection query to populate the product view model
List<ProductViewModel> products = (from prod in context.T_Projects
where prod.T_ProjeCategory.KategoriAd == Category
select new ProductViewModel()
{
Baslik = prod.Baslik,
Icerik = prod.Icerik,
ProductId = prod.ID,
ResimYolu = prod.ResimYolu
}).ToList();
return Json(products);
}
public class ProductViewModel
{
public int ProductId { get; set; }
public string Baslik { get; set; }
public string Icerik { get; set; }
public string ResimYolu { get; set; }
}
Feel free to modify the example to fit your needs. Remember to use the debugging tools and make at least an attempt to troubleshoot.
Your onclick event does not put the id you want to pass, here is my demo:
Model:
public class Cat
{
public int Id { get; set; }
public string KategoriAd { get; set; }
public ICollection<TProject> TProjects { get; set; }
}
public class TProject
{
public int Id { get; set; }
public string ResimYolu { get; set; }
public string Baslik { get; set; }
public string İcerik { get; set; }
public int CatId { get; set; }
public virtual Cat Cat { get; set; }
}
I don't recommend that you use your way ,because it will generate multiple tags with the same id in your page, which doesn't match the id is only principle, you could use js splicing, like this:
Member
21 Points
290 Posts
Asp.Net Core Category - Product
Sep 14, 2019 01:31 PM|Aliyilmaz|LINK
Hi,
I am using .net core.
I have 2 tables that I want to make connected. According to the category table I want to come to the product table. How can I do it.
Index.csHtml
Home Controller
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 14, 2019 04:59 PM|mgebhard|LINK
Forum members cannot see your table design. How is the category joined to the product table?
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 14, 2019 05:14 PM|Aliyilmaz|LINK
Hi ,
I show table desing and shared my code .
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 14, 2019 05:36 PM|mgebhard|LINK
Two link queries are shared without any indication how the tables are joined. Once again, can you share the table schema?
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 14, 2019 05:54 PM|Aliyilmaz|LINK
Hi,
I shared database schema.
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
Contributor
5011 Points
1968 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 12:39 AM|KathyW|LINK
You have no image in your posts.
You are linking to an image that results in a "Forbidden" error.
Don't use an image - just post the table structure.
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 10:41 AM|Aliyilmaz|LINK
What can I do more ? How is post table structure. I explained the problem enough
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 11:13 AM|mgebhard|LINK
Share the table schema, entities, and/or fluent source code.
You've explained the problem but have not shared enough information for the community to provide an accurate solution. The code you shared....
...queries two tables. The code does not indicate if the tables are related in any meaningful way. The fact that you think you've provided enough code indicates you need to spend more time learning the fundamentals.
Products and Categories is a standard learning pattern. Usually there is a third table ProductCategory that contains a ProductId and CategoryId. A basic LINQ to Entities query can produce the products in a category. This concept is covered in just about every get started EF Core tutorial.
If you want help from the community then we need to see your table schema otherwise we are just guessing what bugs exist in your design and code. Another option is setting aside time to learn LINQ to Entities fundamentals,
https://docs.microsoft.com/en-us/ef/core/modeling/
https://docs.microsoft.com/en-us/ef/core/modeling/relational/tables
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 11:28 AM|Aliyilmaz|LINK
But I am using Entity DB First. So I shared DB Diagram, and code. I want to do explained.
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 11:48 AM|mgebhard|LINK
We cannnot see the image as explained by Kathy. You'll need to publish the image on the internet and link to the image.
Second, this is an EF Core support forum and EF Core does not have the DB first feature only Code First. Are you in the wrong forum?
It's very puzzling why you are unable to explain the Product and Category design. Are you following a tutorial or downloaded the database? If so, point us to the tutorial. If you designed the tables yourself, which is how DB first works, then you should be fully aware of how you designed the schema and how the IDs are related.
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 12:56 PM|Aliyilmaz|LINK
Hi,
You dont see my post picture ? I create table sqlFiddle.
http://sqlfiddle.com/#!18/c5ce8/3
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 01:34 PM|mgebhard|LINK
Correct we cannot see the images.
The following is a basic JOIN using SQL.
Your link query will be similar to the following.
I simply scaffolded the View using tools in Visual Studio
But, you still have not provided the EF version you are using. The example above is from EF 6 because, as stated above, EF Core does not have a DB first. I assume you posted in the wrong forum.
Lastly, I strongly recommend that you go through the Getting Tutorials found on this site. The code shown above is a fundamental concept that you need to understand before moving forward.
https://docs.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/database-first
Contributor
3991 Points
3363 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 01:40 PM|DA924|LINK
A Project can only have one Category. It can't have multiple Categories. It looks to me that all you need is a CategoryID column on the Project table definition.<br>
<br>
Just what are you trying to accomplish?
Are you using EF Core?
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 01:42 PM|Aliyilmaz|LINK
I know create database with MVC. But I want to do dynamic jquery mixitup, Can you see first post my code.
For example : https://codepen.io/SitePoint/pen/zxEdQr/
Index.cshtml
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 02:07 PM|mgebhard|LINK
So, your actual question is how to use a 3rd party library name mix it up? At the same time, it seems you do not understand EF and MVC fundamentals which is causing the confusion with the JavaScript Library. Build a View Model to shape the data for use with mix it up. Read the mix it up documentation so you understand how to shape the View Model.
https://www.kunkalabs.com/mixitup/
In the future, please be more explicit about the problem you are trying to solve. This allows the community to provide accurate assistance without having to ask several several clarifying followup questions.
Contributor
3991 Points
3363 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 02:08 PM|DA924|LINK
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 02:18 PM|Aliyilmaz|LINK
Hi DA924,
Yes, someone finally understood.
Please. I shared my code and database schema. Could you share an appropriate example.
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 02:40 PM|mgebhard|LINK
Craft a simple LINQ query that filters by category.
Sample URL.
https://localhost:44391/?category=Web%20Site
You can also do this with a post. The query stays the same.
Again, these concepts are openly covered in any Getting Started with MVC tutorial.
Contributor
3991 Points
3363 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 02:44 PM|DA924|LINK
Then if you want to have view that allows to select all Projects belonging to a particular Category selected from the dropdownlist, you can take the selected CategoryID and query the Project table for all Project records the have the CategoryID in the Project record
The key I think is that you need a CategoryID on the Project table
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 02:55 PM|Aliyilmaz|LINK
Hi mgebhard,
I did like your example. But I want to do with jquery. I dont want page is postback. How can I do ?
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 03:08 PM|mgebhard|LINK
You change the requirements with each new post. At this point you have not shared a single line of jQuery code. I think you should try writing the jQuery/AJAX code yourself. Share your code if you run into trouble. Also, I think you are trying to use a 3rd party library and need to read the documentation.
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 03:44 PM|Aliyilmaz|LINK
Hi,
When I click the button but dont get json result.
Index.cshtml
Layout.cshtml (Javascript)
Home Controller
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
All-Star
43781 Points
18758 Posts
Re: Asp.Net Core Category - Product
Sep 15, 2019 06:02 PM|mgebhard|LINK
There's no response because your code does not submit the Id. If you were using debug tools like Dev Tools you should see errors. Also if you set a break point in VS you'll notice the action is not hit.
IMHO, your filter design is a little confusing too. Anyway, below is a basic example that builds my previous posts.
View
Model
Feel free to modify the example to fit your needs. Remember to use the debugging tools and make at least an attempt to troubleshoot.
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 21, 2019 07:08 PM|Aliyilmaz|LINK
Hi mgebhard,
Can you do example with my code please. So when I click category button according to product get data after append html textbox. How can I do ?
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 25, 2019 05:11 PM|Aliyilmaz|LINK
Can anyone help me ?
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
Member
160 Points
88 Posts
Re: Asp.Net Core Category - Product
Sep 27, 2019 06:41 AM|Lewis Lu|LINK
Hi Aliyilmaz,
Your onclick event does not put the id you want to pass, here is my demo:
Model:
View:
Controller:
Best Regards ,
Lewis
Member
21 Points
290 Posts
Re: Asp.Net Core Category - Product
Sep 28, 2019 12:41 PM|Aliyilmaz|LINK
Hi Lewis,
I changed the code a little. Because when you press the button, projects will come according to that category id. I do not come id that way.
Also how to get projects by category. Isn't jquery right?
Index.cshtml
Jquery,
https://www.portakalahsap.com
https://www.portakalahsap.com/ofis-mobilyalari
Member
160 Points
88 Posts
Re: Asp.Net Core Category - Product
Sep 30, 2019 09:33 AM|Lewis Lu|LINK
Hi Aliyilmaz,
I don't recommend that you use your way ,because it will generate multiple tags with the same id in your page, which doesn't match the id is only principle, you could use js splicing, like this:
Index:
<div class="filter-items"> <div class="row"> <div id="propart"></div> </div> </div>
Jquery:
@section Scripts { <script type="text/javascript"> $(document).ready(function () { }); function GetProje(id) { $.ajax({ type: 'POST', url: '/Product/GetProje/', contentType: "application/json; charset=utf-8", dataType: 'json', data: { id: id }, success: function (data) { var html = ""; for (var i = 0; i < data.length; i++) { html += '<div class="mix col-lg-4 col-md-4 col-sm-12 col-12' + data[i].catId + '">' +
'<div class="box-item">' + '<h3 class="item-name mb-0">' + data[i].baslik + '</h3><span class="cat-name">' + data[i].İcerik +
'</span></div></div>'; } html = html.replace(/null/g, "").replace(/undefined/g, ""); $("#propart").empty(); $("#propart").html(html); }, failure: function (response) { }, error: function (response) { } }); } </script> }
Best Regards ,
Lewis