2

I'm following js snippet to grab value dynamic number of input elements. I then use that values as a property inside js object which I later sent to mvc controller.

Since I already using Model inside razor which already has necessary properties to send to the mvc controller I wonder how can I grab those dynamic input elements values without using javascript object.

var myArr= [];
for (var i = 1; true; i++) {
    var pTxtBox = $('#PCaption' + i);
    var nTxtBox = $('#PWinners' + i);

    if ((pTxtBox).length) {
        var myObj = {};
        myObj.Name = pTxtBox.val();
        myObj.NWinners = nTxtBox.val();
        myArr.push(myObj);
    } else {
             break;
           }
    }
2
  • The model contains the initial values you send to the view, not the edited properties. Commented Apr 28, 2015 at 6:38
  • ASP.NET MVC get textbox input value Commented Apr 28, 2015 at 6:39

1 Answer 1

0

When you render a page in MVC, The model is populated with whatever you specifically pass into the view. As MVC isn't coupled with the server. The only time the model re-binds is when there is a post back, then it goes through the input fields with the correct 'name' attribute and binds them into the model expected into the HTTPPOST method in the controller.

You will not be able to dynamically get a textboxes value on the client side using razor, Youll have to get the element by id or class, just as you are doing here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.