locked
MVC 4 dynamic partial views and posted models RRS feed

  • Question

  • Hi,

    We are building an MVC 4 site with razor.  We have a view on the site that has a dropdown.  When one of the values is selected in this dropdown, some simple jquery calls a controller action which dynamically returns some data has a partial view.

        function change() {
            
            var value = $("#DataSourceId").val();

            if (value == 0 || isNaN(value))
            {
                // clear the div
                $("#Data").empty();
            }
            else {
                $('#Data').load("@Url.Action("GetDatasourceData" , "Profiles" )", { id: value }, null);
            }

        };


    This data is used to populate a checkbox list with n values, so that a user can select one or more items they require.  It returns them like this:
                var model = new DatasourceData
                    {
                        ProfiledDataSourceList = pdsList,
                        AvailableSchemaList = pdscList
                    };

                return PartialView("Partials/_Data", model);


    These items are used to build a list of checkboxes one of more of which could be selected by a user.  

            @Html.CheckBoxListFor(x => Model.Items,
                                    x => x.AvailableSchemaList,
                                    x => x.Value,
                                    x => x.Name,
                                    x => x.SelectedSchemaList,
                                    Position.Vertical)            

    The partial view is wrapped in a form submit block.  The aim is that when a user submits the form, their selections are captured by an action that has been decorated with a HttpPost attribute.

        @using (Html.BeginForm())   

    {        

    ...        

    <input type="submit" id="submit" name="submit" value="Create"/>    

    }



                        

    We have a controller action that starts like so:

            [HttpPost]       

    public ActionResult Create(ProfileModel model)



                        
    We would expect to have the selected items returns as part of the model parameter to the Create action.  What we find however is that no items are populated in our expected lists. When we inspect the post via Firebug, we find that checkboxes list selections do not appear to be submitted back to the action. I don't know if this is a problem with checkboxes selection or if it's because we're dynamically building the view/partial views at runtime, so when a post happens - the model can only be populated with objects that we on the page when it was loaded (rather that any dynamically created content).

     Is there a better way of doing this?

    • Edited by ossent Tuesday, July 2, 2013 9:52 PM
    • Moved by Jason Dot Wang Wednesday, July 3, 2013 2:39 AM This thread is about ASP.NET
    Tuesday, July 2, 2013 9:51 PM

Answers

  • Hi,

      Welcome to MSDN Forum Support.

     

      You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Just Karl Friday, January 17, 2014 10:38 PM
    • Marked as answer by Just Karl Sunday, January 26, 2014 12:54 AM
    Wednesday, July 3, 2013 2:38 AM
  • Hello,

    This should be asked in the ASP.Net MVC forum on forums.asp.net.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C40686F746D61696C2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    • Proposed as answer by Just Karl Friday, January 17, 2014 10:38 PM
    • Marked as answer by Just Karl Sunday, January 26, 2014 12:54 AM
    Wednesday, January 15, 2014 11:15 PM

All replies