Creates new object instead of updating it
Hello, guys, I have a very simple MVC WebApp and I faced a problem. So thing is for some reason my edit method creates new instance in database instead of updating given object. From what I can tell issue is within my html form, because if I add input field for Id there it works fine.
40 Replies
Show code
My bet's you're sending a whole database entity to the backend, instead of using a DTO
And EF interprets the empty ID as your desire to insert a new entity
uff
hold on
discord blames my message for being too long
this is my html
so if I remove this input field for Id, it will create new object
And the backend code? The controller that handles this form's submission?
yea
second
I'm gonna guess that
Movie
is the very same type as your database model for a movieso
MovieCategoryVM
is class to pass more than 1 model to view and back
yesUse a DTO instead
A class that contains only the properties you expect the form to send
I don't see you using any other properties of
MovieCategoryVM
here either, so maybe use just that DTO
That form only sends movie data after allI use those on the view itself, it contains list of categories for movies
Cool
The data you send on GET does not have to be the same as the data you receive on POST
The form will send only those properties, so handle only those properties
The controller action that handles the form submission doesn't need to know the entire list of categories
ah so, I'd create new class, where I will avoid some props I don't need to file like Id and add object of this class as parameter in
Edit
method?Use just this object as a parameter of
Edit
yes
and how can I transfer those properties to my database model? The obvious would be to do like
Yep
but is there smarter way?
:harold:
Or
.ExecuteUpdateAsync()
never used this method, will check what it does
thanks a lot!
it looks pretty much same, like you need to write every property separately
Yeah
ok, thanks again! will try to do it using
ExecuteUpdateAsync
methodIt calls the database just once
but hold on haha
While fetch -> update -> save, calls it twice
I have another model where it looks pretty much same, both form and controller methods
and there it works as intended
hope im not asking too much, could u take a look?
🥹
Sure
html
controller
but the thing is in this case I am not using any separate class for ViewModel
Ah, I just noticed you're doing repositories, huh
Is both repositories'
.Update()
method the same?im using
Category
model
yes
ofc
they inherit from same Repo
class where those method are defined
idk if it is proper way to use Repo Pattern lolThe proper way is to not use the repo pattern :KEKW:
yeah heard about that as well
💀
could this be the case?
Not sure tbh
Could be, that EF works when it's just the model, and doesn't when it's nested as a property of another
well let it be :kekw: I'll try to use DTO, cause I guess it is actually proper way to do it?
ye
Database entities should never leave the backend boundary
if it works will change both things to DTO and just forget lol
I just thought of using custom methods like
ToDto
on Model and ToModel
on DTO, is it something eatable?Leaving the model anemic and adding
ToModel
and FromModel
on the DTO is more recommended
Or creating a whole separate class that has a ToModel
method, and some MapToDto
extension method on IQueryable<Model>
so it can be plugged into your LINQ query
Or, as I like to do, an Expression<Func<Model, Dto>>
that can be plugged into .Select()
Uh I am not really sure about my delegate understanding and how to use them properly, so I think I will try to use separate class with extension methods on
IQueryable<Model>
Thanks a lot for your advices!:pikawhat:
Looks scary
Then you just plug it into
.Select()
(model)
is the method parameter here?
ye
Looks way more eatable ngl
Yea definitely worth giving more practice to delegates, will try to work with this as well