在post MVC上获取带有模型的空列表

时间:2017-10-03 21:01:04

标签: javascript jquery asp.net-mvc

我在角色模型中列出了RoleCapability。我在流畅的nhibernate中连接它。直到Itete模型到控制器我得到RoleCapability列表计数为零。

 public class Roles : IEntity
 {
       public virtual int Id { get; set; }


#region Referacies

     public virtual IList<RoleCapabilities> RoleCapabilities { get; set; }

#endregion

    public Roles()
    {

         RoleCapabilities = new List<RoleCapabilities>();

    }
}

以下是控制器功能

[HttpPost]
   public ActionResult AddOrEdit(Roles entity)
    {
        try
        {

            if (ModelState.IsValid)
            {


                if (entity.Id == 0)
                {


                    _RolesService.Insert(entity);
                    return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                   _RolesService.Update(entity);
                    return Json(new { success = true, message = "Updated Successfully" }, JsonRequestBehavior.AllowGet);
                }
            }
        }
        catch (Exception e)
        {
            return Json(new { success = false, message = "Error  " }, JsonRequestBehavior.AllowGet);
        }

        return Json(new { success = false, message = "Error " }, JsonRequestBehavior.AllowGet);
    }

我已经在javascript中检查了表单的结果,所以列表已经填满了数据,但是在将模型送到控制器之后。它在RoleCapabilites列表中是空的

   @model MBA.Core.Entities.Roles
   @{
      IEnumerable<SelectListItem> list1 = ViewBag.CapabilityList1;
      IEnumerable<SelectListItem> list = ViewBag.CapabilityList;

    }
@using (Html.BeginForm("AddOrEdit", "Roles", FormMethod.Post, new { 
enctype = "multipart/form-data", onsubmit = "return SubmitForm(this)" 
     }))

{

<div class="form-group">
    <table>
        <tr>


 <td>
          @Html.ListBoxFor(m => m.Capabilities, list1, new { @class = "form-control", @style = "display:block", @id = 
         "sourceItems" })

        </td>
            <td><input id="ShiftRight" type="button" value=">>" /></td>
            <td><input id="ShiftLeft" type="button" value="<<" /></td>
            <td>
                   @Html.ListBoxFor(m => m.RoleCapabilities, list, new { @class =  
           "form-control", @style = "display:block", @id = "destinationItems" })
            </td>
        </tr>
    </table>
    </div>
    <div class="form-group">
        <input type="submit" value="Submit" class="btn btn-primary" />
        <input type="reset" value="Reset" class="btn" />
     </div>

       }

这是javascript代码

  function SubmitForm(form) {
    debugger;

    if ($(form).valid()) {
        $.ajax({
            type: form.method,
            url: form.action,
            //"datatype": "json"
            data: formData,
            processData: false,
            contentType: false,
            });
    }
    return false;
}

0 个答案:

没有答案