Model properties null on Ajax post from list box c

2019-08-26 02:41发布

问题:

I have a view which contains a list box. when users click on an item in the list box I have to post to a controller action method to see the value of the items selected/de-selected in the list box.

So it is posting to the controller action but the model is posted as null. When I post to the controller, I do serialize the form.

In other pages of my application when I serialize the form and post to the controller, the model is never null. I am not sure whats going on in this page but here is the code.

JS File

  var serviceEntryURL = '@Url.Action("ServiceSystemSelection", "ServiceEntry")';


    $('#systemlstbox').change(function () {
        //        alert('x');
        var overlay = $('<div>loading errorcodes and parts..</div>').prependTo('body').attr('id', 'overlay');
        $.post(serviceEntryURL,
              $("#form").serialize(),
               function (data) {
                   //                   $("#runDatestreeview").remove();
                   //                   $("#testExceptiontreeview").remove();
                   //                   $("#treeview").remove();

                   //                   $("#main").html(data);
                   //                   $("#ErrorCodeDisplay").empty();

               }, "html");
        overlay.remove();
    });

View

@model RunLog.Domain.Entities.ServiceEntry
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm(new { id = "form", enctype = "multipart/form-data" }))
{

    <fieldset>
        <legend>Enter a new Service Log Entry</legend>
        <h3>
        </h3>
        @Html.ValidationSummary(true)
        <div class="exception">@(ViewBag.ErrorMessage)</div>
        <div class="bodyContent">
            <span class="leftContent">
                @Html.Label("Service Request Number")
            </span><span class="rightContent">[Generated] </span>
        </div>
        <div class="bodyContent">
            <span class="leftContent">
                @Html.Label("Service Request Date / Time")
            </span><span class="rightContent">
                @Html.EditorFor(model => model.ServiceDateTime)
              </span>
        </div>
        <div class="bodyContent">
            <span class="leftContent">
                @Html.Label("Technician")
            </span><span class="rightContent">
                @Html.DropDownList("TechnicianID", String.Empty)
            </span>
        </div>
        <div class="bodyContent">
            <span class="leftContent">
                @Html.Label("System")
            </span><span class="rightContent">
                @Html.ListBoxFor(model => model.SelectedSystemIDs, new
                    MultiSelectList(ViewBag.SystemID, "Text", "Value",
                     Model.SelectedSystemIDs), new { id = "systemlstbox", name = "listbox" })
            </span>
        </div>
}

Controller Action

 [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ServiceSystemSelection(ServiceEntry serviceEntry)
        {

}

回答1:

I fixed it, the form serialization line was wrong.

$('#systemlstbox').change(function () {
    //        alert('x');
    $.post(serviceEntryURL,
          $('form').serialize(),
           function (data) {
           }, "html");
});