locked
posting value from multi text box into a single string RRS feed

  • Question

  • User-323149085 posted

    Hello , 

     I have a form that the user can  insert multi items (email address) but I need it to be submitted as a single string to my controller 

    eg 

     <form asp-action="Request" enctype="multipart/form-data" class="form-horizontal " >
    <input type="text" name="RMail" class="form-control text-right textcopy">
    <input type="text" name="RMail" class="form-control text-right textcopy">
    <input type="text" name="RMail" class="form-control text-right textcopy">
    //More items to submit
    <input type="submit" disabled="disabled" value="Send" id="sub1" class="btn btn-success" />
    </form>

    I need to collect all user type in value from RMail into one single string and post to my controller . 

    Thanks

    Wednesday, June 5, 2019 2:05 PM

All replies

  • User753101303 posted

    Hi,

    You really need to do that on the client side ?

    It seems it could be easier to use RMail[0], RMail[1], RMail[2] which should post that as an array. You could then use String.Join (or I would consider maybe to keep that as a list depending on what is done later with that?).

    Wednesday, June 5, 2019 2:36 PM
  • User-323149085 posted

    It seems it could be easier to use RMail[0], RMail[1], RMail[2] which should post that as an array. You could then use String.Join (or I would consider maybe to keep that as a list depending on what is done later with that?).

     Do you think I should do (in my controller ) ? 

    public async Task<IActionResult> Bid_Request(QuettaCatViewModel model , QuettaCatViewModel[] vendorsMails)

    {

    froeach…() 

    }

    instead of getting it in one string ? 

    Wednesday, June 5, 2019 2:47 PM
  • User475983607 posted

    Hello , 

     I have a form that the user can  insert multi items (email address) but I need it to be submitted as a single string to my controller 

    eg 

     <form asp-action="Request" enctype="multipart/form-data" class="form-horizontal " >
    <input type="text" name="RMail" class="form-control text-right textcopy">
    <input type="text" name="RMail" class="form-control text-right textcopy">
    <input type="text" name="RMail" class="form-control text-right textcopy">
    //More items to submit
    <input type="submit" disabled="disabled" value="Send" id="sub1" class="btn btn-success" />
    </form>

    I need to collect all user type in value from RMail into one single string and post to my controller . 

    Thanks

    Inputs with the same name are considered an array.

    Do you think I should do (in my controller ) ? 

    public async Task<IActionResult> Bid_Request(QuettaCatViewModel model , QuettaCatViewModel[] vendorsMails)

    {

    froeach…() 

    }

    instead of getting it in one string ? 

    We cannot see the model and can only guess.  But a model would have the following format.

    public class MyModel
    {
        public string[] RMail {get; set;}
    }

    This input parameter...

    QuettaCatViewModel[] vendorsMails

    expects an array of QuettaCatViewModel but there is no indication in your code that you are submitting an array of QuettaCatViewModel.

    Wednesday, June 5, 2019 3:06 PM
  • User753101303 posted

    I would try both string[] RMail and string RMail (as a parameter or as a model property) to see what happens :
    - according to https://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ values with the same name are already posted as a collection of values out of the box
    Request.Form["RMail"] should return a single comma separated string but I'm not sure what does the default binder

    I'm just trying to avoid doing things such as adding an hidden field and JavaScript code when the small added complexity doesn't seems to bring a clear benefit. If you want to validate each address separately on the server side or to check for duplicate values, you'll likely need anyway to turn this single string to a collection.

    Once done you can easily use https://docs.microsoft.com/en-us/dotnet/api/system.string.join?view=netframework-4.8 if it is really easier to concanete those values for futher processing.

    Wednesday, June 5, 2019 3:19 PM