Pass Switch to Controller

Asked

Viewed 192 times

1

I have been looking for a solution for several days but I haven’t found anything useful to me. The problem is this, I have a Conttroller receiving data from View, so far so good, but when I capture this data it only captures through the database method (table User) but wanted it also to save the id value in an integer.

Public actionResult criar ( Usuario usuario, int id)
{
    //Faz o que tem que ser feito 
// Ele só pega o valor no usuario e no id da como nulll


}
  • you could better exemplify what you would like, because I didn’t understand.

  • So ,in my controller I want to register users ,so far good ,I can but when I sign up again the user with same id it gives error (gives error because can not have two equal id) I am using a logic so that it can receive the id typed by the user passing it to for control by int id ,ai so I can work with the value and manipulate

  • When I get user data my int id is saying it contains no value , if I let publc actionResult create (int? id)from as null and do not receive the data from the vi but with public actionResult create ( user) it receives the data

  • Try to elaborate on your question. > "...only captures through the database method..." this is a little strange, receive the data from the View in the Controller should not have relation to database, put the code of your View and your Model, so it is easier for staff to help.

1 answer

1


In id your field is coming as null because in your view in the form there is no field as id, for this to work you can do as follows:

@Html.HiddenFor(model => model.Id)

Do it inside the Begginform

@Html.BegginForm....

That your id value will arrive in the controller

  • But can you get the id and User value at the same time? in case User would be the User

  • Yes, The ID will come in the User and come in the parameter variable

  • perfect,helped me a lot ,now I can work with the data ,was also wrong when declaring the variable with the same name.

  • Very good to be able to help friend, If possible mark as answered and a useful vote in response, It helps me a lot

  • I don’t have enough scores yet :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.