views:

210

answers:

1

I'm using Linq to SQL within an ASP.NET MVC application. The model consists of a Decisions table with a foreign key link to an Options table (one decision, many options). So the Model allows me to access the Options collection through the Decision.Options property. I want to allow my users to update the Options collection for a Decision in a separate view from updating other Decision properties. What is the best way to pass the Options collection back into the controller in order to update the Decision object? Here's the code:

The controller sends a Decision model to the Options view:

   //
    // GET /Decisions/Options/5
     [Authorize]
     public ActionResult Options(int id)
     {
         Decision decision = decisionRepository.GetDecision(id);
         return View(decision);
     }

My view page just lists the Option's Name field for users to modify and submit:

<% using (Html.BeginForm())
{%>
    <div id="answers">
        <table>
        <% int i = 0; %>  
        <% foreach (var item in Model.Options)
           { i += 1;
         %>
            <tr>
                <td>
                    <label>Option #<%= i%>:&nbsp;&nbsp;</label>
                    <%= Html.TextBox("Option.Name", item.Name)%>
                </td>
            </tr>

        <% } %>
        </table>

        <input type="submit" name="button" value="Back" />
        <input type="submit" name="button" value="Next" />
    </div>
    <% } %>

Here is the controller code for the Options POST action:

     // POST /Decisions/Options/4
     [AcceptVerbs(HttpVerbs.Post), Authorize]
     public ActionResult Options(int id, string button, Decision d)
     {
         Decision decision = decisionRepository.GetDecision(id);

         if (decision == null)
             return View("NotFound");

         if (button == "Back")
         {
             return RedirectToAction("Edit", new { id = decision.DecisionID });
         }

         //Update the options in the decision model             
         //TODO: What's the best way to update the Decision.Options parameter?

         //Save options and move on to factors
         UpdateModel(decision);
         decisionRepository.Save();
         return RedirectToAction("Factors", new { id = decision.DecisionID });
     }

On the POST action for Options, what is the best way to pass back the collection of Option items?

I've tried working with the FormCollection directly to convert to an EntitySet for assignment to Decision.Options before calling the Save method. The code above has the Decision object being passed in, but the Options collection is always null, hence my commented TODO.

I've gotten this to work for editing the Decision object directly, but am having problems with updating a child table in the model using a Model Binder or FormCollection. Any help is greatly appreciated!

+1  A: 

Since you have many potential options you need to distinguish each input from the others. You can't give them all the same name.

Instead of:

 <%= Html.TextBox("Option.Name", item.Name)%>

try using:

  <%= Html.TextBox("Option[" + i + "].Name", item.Name)%>

Then in your action have an Option array as one of the parameters.

public ActionResult Options( int id, string button, Decision d, Options[] options ) { ... }

Then you can do what you need to connection the supplied options to the Decision.

See Hanselman's blog entry on the topic.

tvanfosson
Thanks! I was finally able to get the array of options in the Action method, after I remembered that the array had to be 0 based! I had started them at one. Thanks again!
kwgainey