ASP.NET MVC Model Binding into a List

前端 未结 2 812
粉色の甜心
粉色の甜心 2020-12-18 06:50

In my ASP.NET MVC site, part of a feature allows the user to enter the hours when a certain venue is open.

I\'ve decided to store these hours in a VenueHours table i

相关标签:
2条回答
  • 2020-12-18 07:21

    Scott Hanselman posted a very good article about how the default model binder deals with collections, arrays and dictionaries. It's great if you don't want to write a custom model binder, although writing a custom model binder isn't that big a deal.

    0 讨论(0)
  • 2020-12-18 07:24

    Please try the following steps

    1 - Place all the input boxes (which can be populated by DatePicker JQuery control) inside a div in the aspx view.

    2 - using Jquery selectors to capture all the values(inside the above specified DIV) that needs to be posted to the Action method , into a JavaScript array in the Client Side.

    please refer Filtering input elements inside DIV

    var enteredVenueHours = new Array();
    for(var i=0;i < inputsInsideDiv.length;i++)
    {
        // add the elements to the JS array
    }
    

    3 - Use the Jquery post to send the enteredVenueHours back to the controller action**

    $.post('<%=Url.Action("ActionMethod")%>',{ EnteredVenueHours: enteredVenueHours,function (data)
    {
         // Udpate status to be displayed here. 
    });
    

    4 - Make sure the Controller action method has the following Signature

    [AcceptVerbs(HTTP.Post)]
    public ActionResult ActionMethod(List<string> EnteredVenueHours)
    {
         // Have the DB persistance logic .
    }
    
    0 讨论(0)
提交回复
热议问题