Trying to make it so when two of the same products are added to cart it increments quantity instead
Hello, im rying to make it so when two of the same products are added to cart it increments quantity instead of showing two of the same product with a quantity of one, heres my current code. Its very scuffed as its a test for my main project so it doesnt matter how ugly the code is haha https://paste.mod.gg/vrujthbplsay/3
BlazeBin - vrujthbplsay
A tool for sharing your source code with the world!
46 Replies
this line
if (_cartService.GetCartItems().Any(cartItem => cartItem.Equals(productToAdd)))
doesnt do what you think it does
I suggest you use a SingleOrDefault
and match the ID instead of using Equals
how would be best to do that?
Try it yourself, see what you come up with.
If you can't make it work, show us what you tried.
i tried a few versions of this but to no avail
Stop using
Equals
what do you think Equals
does for two custom defined objects?
I'm assuming you did not implement IEquatable
on your CartItem
classno, i implemeted it earlier but couldnt get it to work so i got rid of it
Look at this
what about it?
Well, do you see how it does roughly the same thing but with a lot less code?
yeah, does it create a new product in the code though, thats what im struggling most with, i can kind of do it if i am creating a new product in the if statement but because i already initialized all my products i find it much harder. but i see what you mean, lots of it is combined into the same lines
thats what my test code looks like
and it prints...
what
Product
looks like doesnt really matter, in this case its just an Id and a Nameah i see
so if you find the product via the ID before adding or not doesnt really matter
i added my other view where the button is pressed to add the item to the cart, only the first button is set up but does that look okay
https://paste.mod.gg/ebnxxfmzfbrc/5
BlazeBin - ebnxxfmzfbrc
A tool for sharing your source code with the world!
?
I don't do MVC, so I'm not looking at the cshtml stuff
oh, sorry i was just thinking it may be a bit of the problem
but nvm
how so?
I mean, if your action is being called with invalid values, thats a problem yes
but surely you used the debugger to make sure that isnt the case? :p
i was just wondering as i had to use chatgpt for most of that so its hard for me to check myself
well uhh
you're using
$.ajax
which is almost definately not recommended these days, and your controllers "AddToCart" action is NOT set up to be an API endpoint
so no, this is not correctim mainly making this project as something to go off for reference as its my first proper asp.net "project" so im trying to make it, even partly with chatgpt & tutorials, then use it for reference for my next projects
an ajax request is done without a pageload event, meaning you cant return a redirect as the result
would that have anything to do with messing up my add to cart
oh
yeah you seem to have not grasped the flow of a website app
so at its core, ASP.NET MVC is purely serversided and serves server side rendered HTML from its controller endpoints
but you've then added javascript to do XHR (ajax is the old way to do this), which is client side.
That itself is fine, but your entire codebase must be written in a way to support this
some endpoints would serve HTML, some would be API endpoints intended to be hit with XHR requests
being honest i just used chatgpt for that, as its my first real asp.net "project" i tried to get some kind of guidance and just followed chatgpts advice blindly
How do you expect to learn if you just chat GPT everything?
I have no interest in reviewing or fixing AI written code. Good luck.
what would you best recommend for learning? i cant just pick up the keyboard and start writing things i dont know, i wish i could but i cant, i watch youtube videos but there isnt an infinite source of videos to watch that arent just mindlessly copying. is there something you would recommend ?
yeah but most of theirs are for blazor
Some, not most.
They cover web api, razor pages and even MVC to some extent.
but yeah, MVC is "the old way", and not really common these days
all jobs around me seem to be looking for MVC but yeah like you say, all the learning material seems to be ancient, at least 7 years+ for the most part
Weird. I've worked with ASP.NET for 10+ years and not once done an MVC project
which do you use?
almost exclusively web apis
frontends these days are usually so involved they need to be their own apps, and javascript is still king there
I've done a razor page app or two as well, when we just wanted a quick-and-dirty admin panel
But don't misunderstand me, MVC is fine as a technology. Its just a bit dated.
it still works fine, especially once you are aware of the limitations to pure SSR and "how to get around it"
is web api the most popular out of the others would you say?
Its what I see most, yes.
its different thou, its not a full app you make with it, just an API
that is then consumed by some other app, maybe a javascript frontend, or a blazor app, or.. something else.
can be almost anything
okay, i may look into switching from MVC to web api as MVC material is all ancient
you might want to look at razor pages, if you still want to make a full web app
its very similar to MVC
and 90-95% of what you learn carries over directly
okay, im planning on learning JS/TS after c#, is that a conflict of interests do you think?
not really?
C# for backend and JS/TS for frontend is a very common tech choice
yeah that was my plan
you'd use C# and ASP.NET to write a web api, then access that API from a React/Angular/Vue/Svelte/Solid/Whatever javascript app
would that work with razor?
soryr if these are VERY stupid questions
no, you'd not use either razor or MVC
those are for serving HTML
meaning you'd not really mix that with a "classic" js frontend
I suppose you could mix it with something like htmx, but thats kinda niche still
so if im planning on learn js/ts after its best to go the web api route ?
they go well together, yes
I don't really see a benefit to learning MVC if you plan on dumping it to use JS/TS immediately afterwards
okay, thank you for all your advice and your patience with my stupid questions, its been really helpful talking to you! i think ill go the web api route then learn js/ts after