Home/ASP.NET Forums/General ASP.NET/MVC/MVC EF Update multiple rows in one form post.

MVC EF Update multiple rows in one form post. [Answered]RSS

13 replies

Last post Apr 30, 2013 07:31 PM by jstus

Francesco Abbruzzese | Blog
Mvc Controls Toolkit | Data Moving Mvc Controls Suite. Launch 25% off!
  • dshrout

    dshrout

    Member

    8 Points

    13 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 15, 2010 07:54 PM|dshrout|LINK

    I didn't understand what you meant until I viewed the source of the rendered page. With the way I was doing it the 'name' and 'id' attributes for every Sequence textbox was "item.Sequence" (same problem with the PromptID textboxes as well). Now each textbox has a 'name' and 'id' equal to its 'value'.

    That is definately something that needed to be corrected so, thank you for that, I do appreciate it, but I am still stuck on my original problem.


    How can I traverse the FormCollection object returned to my controller and manually update the Callflow_Sequence table with the data it contains?

    Don Shrout
    <><
  • francesco abbruzzese

    francesco ab...

    All-Star

    22705 Points

    3615 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 16, 2010 03:31 PM|francesco abbruzzese|LINK

    I see....

    The point is that The for each loop you made is still wrong! 

    Why?

    Because you still give the same name to all your order items! If their name are equal how the model bilder can distinguish them?

    The name YOU gave to your Sequence is: item.Sequence.ToString()

    The name I suggested you is something like" callflowSew[0].Sequence"  "callflowSew[1].Sequence".... and so on

    You can achieve those names by writing: string.Format("callflowSew[{0}].Sequence", i)

    where i is an integer starting from 0 you increment in your foreach loop

    This way the names will be all different and will conform to the convention used by the modelbinder to retrieve collections from the FormCollection. It is not the modelbinder that need to lool on all equal names, but YOU when you write the names need to to loop on different names, by changing the index in:

     " callflowSew[0].Sequence"  "callflowSew[1].Sequence".... and so on

    ModelBinder is able to read collection if their elements have names with indices like the ones i shwed you!

     

    Francesco Abbruzzese | Blog
    Mvc Controls Toolkit | Data Moving Mvc Controls Suite. Launch 25% off!
  • dshrout

    dshrout

    Member

    8 Points

    13 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 16, 2010 05:06 PM|dshrout|LINK

    I kept the old for each loop (it's commented out) to give your first comment context for anyone who comes to this thread later. The second for each loop does actually give each textbox a unique name.

    Here is a "view source" snippet of the rendered page:

    <div>
        <label for="item_Sequence">Sequence</label>
        <input id="1" name="1" type="text" value="1" />
    
    
        <label for="item_PromptID">PromptID</label>
        <input id="pid_0001" name="pid_0001" type="text" value="pid_0001" />
    </div>
    
    
    <div>
        <label for="item_Sequence">Sequence</label>
        <input id="2" name="2" type="text" value="2" />
    
    
        <label for="item_PromptID">PromptID</label>
        <input id="pid_0002" name="pid_0002" type="text" value="pid_0002" />
    </div>
    
    
    <div>
        <label for="item_Sequence">Sequence</label>
        <input id="3" name="3" type="text" value="3" />
    
    
        <label for="item_PromptID">PromptID</label>
        <input id="pid_0003" name="pid_0003" type="text" value="pid_0003" />
    </div>

    When I get back to the controller, if I access the FormCollection object (named 'collection') like an array I can see that it does contain the modified data but I just can't figure out how to write it back to my EntityObject.

    Here's a screen shot: (The second call to UpdateModel() is obviously wrong but that's the root of my question)

    EVPClient
     

    Thank you for your patience, I hope I'm not missing an obvious point.

     

    Don Shrout
    <><
  • francesco abbruzzese

    francesco ab...

    All-Star

    22705 Points

    3615 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 16, 2010 11:21 PM|francesco abbruzzese|LINK

    Ok I see,

    You generated unique names but they have not the format the model binder is looking for! The name structure must follow the herarchical structure of objects, The modelbinder try to bind the objects by matching the herarchical structure of objects with names. Model binder expect names be paths in the tree of objects. If you do this you can also avoid to use UpdateModel

     

    For example if I have a Client object that is the top level model and such client ha a property named "Order" containing an order and the order contains a property "OrderId" in order for the modelbinder to retrieve the OrderId you must give to the TextBox containing the OrderId the name: "Order.OrderId". That is the name is obtained by putting together all  property names on the path from the root model to your target property. Now if you have several orders into the Order property because such property is say a List, the model binder expect the names Order[1].OrderId.... names Order[7].OrderId......and so onthat is a general pattern of Order[i].OrderId. If your names are organized this way you don't need to call UpdateModel, but you need just passing your TOTAL MODEL as a unique  parameter to your caontroller.

    You are overcomplicating the matter, you don't need to call updateModel and passing it a prefix at all, you just need to give right names to your fields and then passing a UNIQUE parameter to your controller THE TOP LEVEL MODEL. Do this and avoid getting crazy playing with prefixes ...the MVC engine will do the job for youSmile

    Francesco Abbruzzese | Blog
    Mvc Controls Toolkit | Data Moving Mvc Controls Suite. Launch 25% off!
  • Dirkle

    Dirkle

    Member

    427 Points

    118 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 17, 2010 05:14 PM|Dirkle|LINK

    Hello dshrout,

    Francesco's solution is correct and is what we had to do for a number of our solutions a while ago. This resource is what we used to get our solutions working quickly and easily http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx. Hope that helps.

  • dshrout

    dshrout

    Member

    8 Points

    13 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 22, 2010 04:31 PM|dshrout|LINK

    First, thank you both for your time.

    I've tried everything you've suggested, including the haacked.com article, but I still cannot get my EntityObject updated. When I set a break point in my controller, I can see the modified data in the FormCollection object but I just can't get that data into my viewModel's EntityObject.

    You said... "you don't need to call updateModel and passing it a prefix at all, you just need to give right names to your fields and then passing a UNIQUE parameter to your controller THE TOP LEVEL MODEL."

    I think I now have the fields named correctly (see below), but I'm not sure what you mean by passing a unique parameter to my controller. What should my controller look like? 

    <table>
                <tr>
                    <th>Sequence</th>
                    <th>Prompt Name</th>
                </tr>
                
    
                <tr>
                    <td id="Sequence">
                        Prompt 01
                    </td>
                    <td id="PromptID">
                        <input id="callflowSeq_0__PromptID" name="callflowSeq[0].PromptID" type="text" value="pid_0001" />
                    </td>
                </tr>
    
    
                <tr>
                    <td id="Sequence">
                        Prompt 02
                    </td>
                    <td id="PromptID">
                        <input id="callflowSeq_1__PromptID" name="callflowSeq[1].PromptID" type="text" value="pid_0002" />
                    </td>
                </tr>
                
    
                <tr>
                    <td id="Sequence">
                        Prompt 03
                    </td>
                    <td id="PromptID">
                        <input id="callflowSeq_2__PromptID" name="callflowSeq[2].PromptID" type="text" value="pid_0003" />
                    </td>
                </tr>
            </table>

    Again, thank you for your time, I really do appreciate it.
     

    Don Shrout
    <><
  • francesco abbruzzese

    francesco ab...

    All-Star

    22705 Points

    3615 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 22, 2010 04:45 PM|francesco abbruzzese|LINK

    Hi,

    In order for the above to work, your  class

    public class MarketsEditViewModel
    04.    {
    05.        public Client_Data clientData { get; set; }
    06.        public List<Callflow_Sequence> callflowSeq { get; set; }
    07.    }


     

    should be the only argument of your controller, if you have other properties to add please add it to the above class try this: this way It MUSK run. if not, please do a self caontained project and send to my email I will debug it to see what the problem is.

    Normally you don't need to care too much about name if you use the m=> m .property overloads of the helpers, because if you do it the helper write the right name for you.However in cade you use a list m =>callflowSeq[i].PromptID is not efficient since in a list it is not possible to take efficiently an element from the middle, so it is better you use a foreach loop that simply scan the list and then put both the name and the value manually in the model

    Francesco Abbruzzese | Blog
    Mvc Controls Toolkit | Data Moving Mvc Controls Suite. Launch 25% off!
  • Dirkle

    Dirkle

    Member

    427 Points

    118 Posts

    Re: MVC EF Update multiple rows in one form post.

    Dec 22, 2010 04:45 PM|Dirkle|LINK

    Try adding ICollection<TYPE> callflowSeq to your action method signature instead of using FormCollection. Then iterate through the collection to get your data.

  • dshrout

    dshrout

    Member

    8 Points

    13 Posts

    Re: MVC EF Update multiple rows in one form post.

    Jan 05, 2011 03:42 PM|dshrout|LINK

    Thanks everyone. My manager has requested that I NOT use MVC for this project so I am shifting to Web Forms. I plan on revisiting this once the app is in production because I really like MVC and I want to make this work.

    Again, thanks for all your help, it is much appreciated.

    Hope to be back soon. 

    Don Shrout
    <><
  • jstus

    jstus

    Member

    30 Points

    41 Posts

    Re: MVC EF Update multiple rows in one form post.

    Apr 30, 2013 07:31 PM|jstus|LINK

    Here is example with FormCollection http://www.youtube.com/watch?v=-lQr1zaVBEQ

  • ‹ Previous Thread|Next Thread ›