Reputation: 6549
I am using the ASP.NET forms model binding that was added in .NET 4.5. I see that there are many things about model binding that is supposed to make it easier and reduce how much code you need to write. However, I want to know if there is a way you can update items manually.
I have found the TryUpdateModel method like so:
public void UpdateEquipment(int Id)
{
var equip = EquipCondContext.Equipments.Single(x => x.Id == Id);
TryUpdateModel(equip);
}
but, I would like to have the ability to do this:
public void UpdateEquipment(int Id)
{
var equip = EquipCondContext.Equipments.Single(x => x.Id == Id);
//equip.Description = A TextBox.Text on that row that I just saved in my list.
EquipCondContext.SaveChanges();
}
Is there a way I can do a more manual kind of updating like that?
Here is an example scenario. Lets say I have a textbox on a row where a user enters a user ID, but on the update I want to actually set their badge number. In my update function, I first want to do a query to get the badge number for the given user ID and then set this property in my entity before saving the context.
Upvotes: 1
Views: 589
Reputation: 12998
What you are trying to do is hooking into the model binding process and changing values before they are saved. I am not sure if the model binding in Webforms is 1:1 the same as in MVC, but you could try this article:
The article is about validation, but you could just as easily change the values there.
Basically what you do is to register a custom ModelBinder for your class which then hooks into the call to bind the model.
Upvotes: 1
Reputation: 12998
If you want to track changes in the Textbox manually you have to listen to the Textbox.TextChanged
event.
<asp:Textbox ... OnTextChanged="MyTextBoxOnTextChanged" />
There you can do with the textbox value whatever you want, in your case store it as the according equipment description.
Upvotes: 0