locked
How to see the textbox text as soon as it changes in another textbox bound with same model property using MVC Model? RRS feed

  • Question

  • User-696098127 posted

    Hi Team,

    Just for clarity, I have taken the simplified form to understand the behavior:

    I have one form in MVC view which is bound with one Model having one property as ReservingPartyName

    Suppose there are 2 textboxes as below:

    @Html.TextBoxFor(a => a.ReservingPartyName)

    @Html.TextBoxFor(a => a.ReservingPartyName)

    You can see that both the textboxes are mapped with model property ReservingPartyName, I want if one of the textbox changes it should automatically change the text in another textbox without jquery.

    Thanks in advance for your answer.

    Prateek

    Monday, July 20, 2020 7:02 AM

All replies

  • User-474980206 posted

    You don’t have to use jquery, but you must use JavaScript to achieve this.

    note: As the browser will post two fields, it will be an collection / array  in the post back model.

    Monday, July 20, 2020 2:56 PM
  • User1686398519 posted

    Hi prtk12,

    • Do you mean without jquery include javascript?
    • As far as I know, if you don't use javascript, your needs are almost impossible to achieve.If you can accept to use javascript, you can refer to the following code.
    @Html.TextBoxFor(model => model.name, new { id = "name1",onkeyup="b()" })
    @Html.TextBoxFor(model => model.name, new { @id = "name2" ,onkeyup = "a()" })
    <script type="text/javascript">
        function a() {
            var val = document.getElementById("name2").value;
            document.getElementById("name1").value = val;
        }
        function b() {
            var val = document.getElementById("name1").value;
            document.getElementById("name2").value = val;
        } 
    </script>
    

    Here is the result.

    Best regards,

    Yihui Sun

     

    Tuesday, July 21, 2020 5:08 AM