MULTIPLE TEXTBOXES FOR ONE FIELD

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
 1505  View(s)
Ratings:
Rate this:

If I have 3 text boxes for 3 field which need to be appended together, how would you go about doing this?

for example:

@Html.TextBoxFor(model => model.PhoneNumber, new { @id = "PhoneNumber1"})

@Html.TextBoxFor(model => model.PhoneNumber, new { @id = "PhoneNumber2"})

@Html.TextBoxFor(model => model.PhoneNumber, new { @id = "PhoneNumber3"})

but when I call model.PhoneNumber in the controller, I only get the first text box value.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: multiple textboxes for one field

    Hi Goti,


    You would need multiple phone numbers in your model phoneNumber1, phoneNumber2, phoneNumber3 as you can only bind one element of one type to the model.

    Alternatively you can use FormCollection

    use standard imports instead of model binding

    <input type="text" value="" name="phoneNumber" id="phoneNumber1" />
    <input type="text" value="" name="phoneNumber" id="phoneNumber2" />
    <input type="text" value="" name="phoneNumber" id="phoneNumber3" />
    Then in your Controller
    [HttpPost]
    public ActionResult Index(MyModel model, FormCollection frm){
    //get elements from form
    String[] numbers = frm["phoneNumber"].Split(',');
    // you will have your 3 numbers
    }

      Modified On Apr-04-2018 03:44:58 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!