Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
I am begginer to MVC.In View there are two textboxs.When i will change first textbox value at that time second textbox value also alter depending on first textbox value..So How to write text change event for textbox in mvc.
Thanks...
Posted

This is a javascript question. You can use jquery to set up events for text change, and to change the text in your other control at the same time. MVC has literally nothing to do with it, it's an engine for creating HTML and javascript, and in this case, you want to write the javascript directly.

The other person has given a wrong answer. This is what you'd do:

$('#eventType').change(function() {
$('#text2').text($('#text1").text());
}
});

Assuming .text is right ( I thought it was .val ), and assuming your two textboxes have ids of text2 and text1.
 
Share this answer
 
v2
Comments
Rohini Shirke 20-Jan-14 7:12am    
Thanks..
I have write java script function.Following is my java script function
<script type="text/javascript">
function myFunction()
{
var id1 = $("#TbFrom").val();
window.location="/MassConversion/demoFun?id="+id1;

}

My View is
<td>
<input type="text" id="TbFrom" value="@TempData["TbFromVal"]" öninput="myFunction()" />
</td>
<td>
<input type="text" id="TbTo" value="@TempData["TbToVal"]" />
</td>
From java script i will call function name "demofun".
public ActionResult demoFun(double id)
{
@TempData["TbFromVal"] = id;
@TempData["TbToVal"] = id*100;
return View("Index","_Layout");

}
Problem is When i change TbFrom value then master page(_Layout.cshtml) layout will be change.That means css will not be applied to Master page(_Layout.cshtml) after demo fun will executed.
Christian Graus 20-Jan-14 13:43pm    
Your script changes the URL. That's not what you said you wanted to do at all ? Why does the master page change when the id changes ? If you're using MVC, why are you using the query string ? How is @TempData a valid variable name in C# ? Why are you setting server side variables ? Shouldn't you create a viewmodel and pass it to the view ? You need to explain more, this looks broken to me, or at least, like you're not using MVC properly
Member 13478674 27-Oct-17 5:24am    
12
Rohini Shirke 21-Jan-14 3:08am    
Thnks..
In my controller i was use @Tempdata["TbTo"] (reason for that I want when i enter input in "TbFrom" textbox & performing some operation on that textbox value.result will be displayed in "TbTo" textbox.
Christian Graus 21-Jan-14 3:18am    
That's pretty useless. @Tempdata["TbTo"] is not a client side construct. You need to post the actual code, all of it, I think, because it doesn't seem like you know what you're doing.
If you use JQuery, this can be achieved quite easily.

JavaScript
$('#eventType').change(function() {
        $('#texr2').text('New Text');
    }
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900