Solved

Is it possible to use a TextBoxFor to send value to two fields in a model

Posted on 2015-01-03
15
162 Views
Last Modified: 2015-01-05
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
0
Comment
Question by:Niall292
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 7

Accepted Solution

by:
Camillia earned 500 total points
ID: 40529667
You need to create a basemodel. I recently had a similar question. Let me see if I can find my question.

I created a base model class for fields that I needed to have in several models.
0
 
LVL 7

Assisted Solution

by:Camillia
Camillia earned 500 total points
ID: 40529670
0
 

Author Comment

by:Niall292
ID: 40529715
I don't know if it is the best way but I just used a bit of jquery to put the value into a hiddenFor and pointed it to the other field
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Niall292
ID: 40529716
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
0
 

Author Comment

by:Niall292
ID: 40530576
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
0
 
LVL 7

Expert Comment

by:Camillia
ID: 40530577
No problem. How did you do it with jquery?
0
 

Author Comment

by:Niall292
ID: 40530581
I just put a hiddenFor control with an ID and put another ID on the TextBoxFor then

$("#TextBox").FocusOut( function () {
$("#Hidden").val($("#TextBox").val())
});
0
 

Author Comment

by:Niall292
ID: 40530587
forgot the ; at the end of $("#Hidden").val($("#TextBox").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(function () {
       $('#hidMerID').val($('#txtMerID').val());
});
0
 
LVL 7

Expert Comment

by:Camillia
ID: 40530593
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.
0
 

Author Comment

by:Niall292
ID: 40530602
The hiddenField I made it a HiddenFor and it did keep it but which might make a difference I am using MVC.
0
 
LVL 7

Expert Comment

by:Camillia
ID: 40530612
I'm using MVC too. How did you define the HiddenFor element?
0
 

Author Comment

by:Niall292
ID: 40530625
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
0
 
LVL 7

Expert Comment

by:Camillia
ID: 40530643
Thanks, I'll see if it works for me
0
 

Author Comment

by:Niall292
ID: 40532647
Camilla,
Did you try it
0
 
LVL 7

Expert Comment

by:Camillia
ID: 40532668
Yes, it worked. Thanks for your help.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question