开发者

Looking for a common pattern with MVC2/3 - bulk view/model update

I've researched a couple of different scenarios as to the best method for performing a bulk update (view to model) for a list of view-bound objects but haven't found a real clear methodology as to the 'best' way to accomplish this. I am new to MVC and am teaching mys开发者_JAVA百科elf by going back to some of our internal applications and recreating them under MVC 2. I've found it easy to handle single object updates but multiples are giving me some trouble.

A little background...

Say you have a view whose model object is a List. When the page is rendered, the view iterates the model's collection and builds a single form with a series of textboxes, labels, other UI-related stuff... Think about a timesheet showing the days of the week and two fields displaying 'hours' and 'overtime hours'. The rendered page has a single html form whose action is 'update' and takes an argument of List. There is a single input that calls this action. The intended behavior is that the view sends all the objects back to the controller for update. For example, if a timesheet was loaded on Friday with hours from Tuesday being completed prior, the user could change Tuesday's hours, add more hours to other days of the week, remove hours, etc... all from the single 'submit' button.

I can obviously do single updates but I am looking for a way to do this in bulk.

I've cobbled together a quick example that I would like to solve before tackling the timesheet app.

Example controller action:

public ActionResult Index()
    {
        List<SampleModel> data = _repo.List();
        return View(data);
    }

Example 'save' action:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(List<SampleModel> mod)
    {
        //Save code removed             
        return RedirectToAction("Index");
    }

Sample View form being constructed:

 <% using (Html.BeginForm("Save", "Home"))//,new {Collection= Model}
   { %> 

<div class="cont">

<% for (int i = 0; i < Model.Count; i++)//foreach (var a in Model)
   {
       var a = Model[i];%>

        <div class="child">
            <div class="header">
                <img alt="" src="../../Img/no.png" class="close" />
                <h3><%: Html.Label(a.Name)%></h3>
            </div>
            <div class="info">
                <%: Html.TextAreaFor(r => a.Msg) %>
                <br />
                <% for (int t = 0; t < a.Hours.Count; t++)
                   { %>

                   <p>
                        <%: Html.Label("Hours") %>
                        <%: Html.TextBoxFor(m => a.Hours[t]) %>
                   </p>

                <%} %>
            </div>

        </div>

<% } %>


<br class="clr" />

<input type="submit" value="Save Changes" />

</div>

<% } %>

Although this sample doesn't exactly recreate what I'll need to do, it is close enough to give the general idea. We've got a model with a generic list, each object in the list has an editable property and collection of objects which can be edited as well.

When the controller method is called, the list passed back is null. In examining the Fiddler trace, I see the namevalue pairs coming back but they are prefaced by the iterator's local variable reference (ex: a.Msg=update&a.Hours%5B0%5D=1&a.Hours%5B1%5D=1&a.Hours%5B2%5D=12&a.Msg=Hello+from+id+%23+1&a.Hours%5B0%5D=39&a.Hours%5B1%5D=43&a.Hours%5B2%5D=21&a.Msg=Hello+from+id+%23+2&a.Hours%5B0%5D=18&a.Hours%5B1%5D=41&a.Hours%5B2%5D=26&a.Msg=Hello+from+id+%23+3&a.Hours%5B0%5D=27&a.Hours%5B1%5D=9&a.Hours%5B2%5D=28).

From this I gather the controller cannot create the List. Could someone point me in the direction as to how to perform this type of 'bulk' operation?

Thanking all in advance!


How to bind to a list http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜