Niall Gallagher
asked on
Is it possible to use a TextBoxFor to send value to two fields in a model
I have a Create View which I am using a ViewModel to send the values to two classes but there is 2 fields which are needed in both classes so rather than making somebody fill in these 2 fields twice I am trying to see if it is possible to use it to write to both classes in the model.
I have tried a few ideas but it seems I'm trying to aim in the dark.
An example of what I am thinking but is
@Html.TextBoxFor(model => model.class1.customerID, model => model.class2.CustomerID)
which I know wont work but it gives an idea what I'm trying. I would greatly appreciate anyone who can guide me in the right direction how to go about this.
Thanks in advance
I have tried a few ideas but it seems I'm trying to aim in the dark.
An example of what I am thinking but is
@Html.TextBoxFor(model => model.class1.customerID, model => model.class2.CustomerID)
which I know wont work but it gives an idea what I'm trying. I would greatly appreciate anyone who can guide me in the right direction how to go about this.
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry Camilla I didn't see your response before I answered myself. I will look at you reply tomorrow as I would prefer not to use jquery if possible
ASKER
Camilla,
I was told your idea would work but I was also told I should go the jquery way so I still think your way might be tidier so I am giving you the points
I was told your idea would work but I was also told I should go the jquery way so I still think your way might be tidier so I am giving you the points
No problem. How did you do it with jquery?
ASKER
I just put a hiddenFor control with an ID and put another ID on the TextBoxFor then
$("#TextBox").FocusOut( function () {
$("#Hidden").val($("#TextB ox").val() )
});
$("#TextBox").FocusOut( function () {
$("#Hidden").val($("#TextB
});
ASKER
forgot the ; at the end of $("#Hidden").val($("#TextB ox").val() );
also what I wrote is not in the right casing. (Guess who was a VB programmer)
the actual code I used was
$('#txtMerID').focusout(fu nction () {
$('#hidMerID').val($('#txt MerID').va l());
});
also what I wrote is not in the right casing. (Guess who was a VB programmer)
the actual code I used was
$('#txtMerID').focusout(fu
$('#hidMerID').val($('#txt
});
Is the value of that hidden field preserved between postbacks? I need to do something similar and wasn't sure if the value gets preserved. I'll try it.
ASKER
The hiddenField I made it a HiddenFor and it did keep it but which might make a difference I am using MVC.
I'm using MVC too. How did you define the HiddenFor element?
ASKER
Here is both the textbox and hiddenfield
@Html.TextBoxFor(model => model.class1.merID, new { @id = "txtMerID"})
@Html.HiddenFor(model => model.class2.merID, new {@id = "hidMerID"})
remember both of these classes are in the ViewModel
@Html.TextBoxFor(model => model.class1.merID, new { @id = "txtMerID"})
@Html.HiddenFor(model => model.class2.merID, new {@id = "hidMerID"})
remember both of these classes are in the ViewModel
Thanks, I'll see if it works for me
ASKER
Camilla,
Did you try it
Did you try it
Yes, it worked. Thanks for your help.
ASKER