0

I have a form with checkboxes and posting them back to the controller and saving them to as SQl Server database, the values are System.String[]. Been looking around forums for a bit looking for a solution to output the array back out as a list of selected equipment but not finding anything. When hovering the array in the controller argument, all values that were selected in the form are listed and correct.

 <div class="checkbox">
   <label>
   <input type="checkbox" name="equipmentCheckbox" id="pc" value="PC">PC
   </label>
  </div>

<div class="checkbox">
   <label>
  <input type="checkbox" name="equipmentCheckbox" id="monitor" value="Flat Panel Monitor">Monitor
   </label>
   </div>

<div class="checkbox">
   <label>
  <input type="checkbox" name="equipmentCheckbox" id="mic" value="mic">Mic
   </label>
 </div>

<div class="checkbox">
   <label>
  <input type="checkbox" name="equipmentCheckbox" id="speakers" value="speakers">Speakers
   </label>
 </div>

<div class="checkbox">
   <label>
  <input type="checkbox" name="equipmentCheckbox" id="videoPlayer" value="videoPlayer">Video player
   </label>
 </div>

<div class="checkbox">
   <label>
  <input type="checkbox" name="equipmentCheckbox" id="lab" value="lab">Lab
   </label>
 </div>

Controller:

[HttpPost]
    public ActionResult equipmentRequest(string[] equipmentCheckbox) 
    {
        var db = new Entities();


        string[] equipment = Request.Form.GetValues("equipmentCheckbox");

        var order = new Order
        {

            EquipmentRequested = equipment.ToString(),

        };

        db.Requests.Add(order);
        db.SaveChanges();


        return RedirectToAction("Index");
    }

I'm stuck on how to parse the array to a list to save correctly to the database instead of System.String[]

1
  • what is the type of EquipmentRequested ? you want to parse the array to a list of what ? Commented Aug 17, 2016 at 17:39

1 Answer 1

2

You're trying to use toString on a string array when you do

equipment.ToString() 

I don't know what your purpose is with that .ToString() call, but there is your mistake.

Order should contain

ICollection<EquipmentRequested> EquipmentRequested { get; set; }

instead of

string EquipmentRequested { get; set; }

and EquipmentRequested should contain a string property 'value' and an OrderId to link the objects.

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

3 Comments

Right. When I removed the .ToString() I'm getting an error of cannot implicity convert type string[] to string.
You need to figure out how you want to store your equipment records in the database, and build a correct datamodel for that. That's missing now, which is the root cause of your problem.
My guess would be that order should have an ICollection of strings as EquipmentRequested instead of a single string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.