asp.net mvc 4 - MVC 4 - null model when trying to post a collection of models -



asp.net mvc 4 - MVC 4 - null model when trying to post a collection of models -

i've looked on , there similar questions trying post collection of models when press submit button complains model null.

i have view in application

view

@model list<business.models.applications.applicantviewmodel> @using gridmvc.html @using (html.beginform("allocatefirst", "application", formmethod.post)) { <div id="grid-wrap" style="width: 500px;"> allocate application <div id="systemmsg"> </div> @html.grid(model).named("firststagegrid").columns(columns => { columns.add(m => m.id); columns.add(m => m.membershipnumber).titled("membership number"); columns.add(m => m.datereceived).titled("registered date"); columns.add(m => m.fullname).titled("name").filterable(true).encoded(false); columns.add(m => m.applicationtype).encoded(false).sanitized(false); columns.add(m => m.assignedstaffmem).encoded(false).sanitized(false).rendervalueas(m => @<b> @html.dropdownlist("ddlstaff", new selectlist(model.first().listofstaff, "userid", "fullname")) </b> ); columns .add() .encoded(false) .titled("action") .sanitized(false) .setwidth(30) .rendervalueas(m => @<b> <a href="#" onclick="assignstaff(@m.id, @html.raw("\"") @m.fullname.trim() @html.raw("\""), @m.id)"> assign</a> </b> ); }).withpaging(5) <br /> assign on bulk: @html.checkbox("assignonbulk") <br /> <div id="bulkassign"> assign to: @html.dropdownlist("ddlstaff", new selectlist(model.first().listofstaff, "userid", "fullname")) @html.hidden("hdnstaffid") <input type="submit" value="submit on bulk" /> </div> </div> <script> $('#assignonbulk').on('change', function () { if ($(this).is(':checked')) { $("#bulkassign").show(); var staffid = $('#ddlstaff').val(); //no :selected here } else { $("#bulkassign").hide(); $("hdnstaffid").val(this); var staffid = $('#ddlstaff').val(); //no :selected here } }); $('#ddlstaff').change(function () { var staffid = $('#ddlstaff').val(); //no :selected here }); $(document).ready(function () { if ($(assignonbulk).is(':checked')) $("#bulkassign").show(); else $("#bulkassign").hide(); }); function assignstaff(appid, fullname) { //get row based on id of grid var tablerow = $("td").filter(function () { homecoming $(this).text() == appid; }).closest("tr"); // staffid based on alternative selected in tr var staffid = $(tablerow).find('option:selected').val(); $.ajax({ type: "get", url: '/application/setfirstcheck', datatype: 'html', data: { "staffid": staffid, "appid": appid }, success: function (success) { $('#systemmsg').html(success); $(tablerow).remove(); } }); } </script> }

what trying if user checks checkbox

assign on bulk: @html.checkbox("assignonbulk")

the div button shown dropdown menu , user can decide assign 1 person people in grid, instead of using actionlink.

what want press post button , posts collection plus staffid dropdown menu. when press post, vs2012 highlights grid , says model null.

controller method

[httppost] public actionresult allocatefirst(list<business.models.applications.applicantviewmodel> model, bool assignonbulk, string ddlstaff) { homecoming view(model); }

i've attached screenshot of happens when click post, highlights entire grid.

well, not possible send list or object controller view. if want post object have fill every property on page ...

you can send info controller able load list there or need utilize ajax , json.stringify.

something like:

<script> var mylist = []; mylist.push({ id: 1, name: 'test', surname: 'test2' }); jquery.ajax({ url: '../controller/action, data: '{ modellist:' + json.stringify(mylist) + '}', type: 'post', cache: false, datatype: 'json', contenttype: 'application/json; charset=utf-8', success: function (data) { }, error: function (data) { } }); </script>

asp.net-mvc-4 post nullreferenceexception

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -