2015-12-02 71 views
0

我试图动态地添加项目到列表中,并使这些项目显示在我的控制器后提交,但现在我甚至没有得到现有的列表 - 出现的项目。MVC - 添加项目到模型中的列表提交

我不是100%问题是什么,但它似乎与每个项目呈现的局部视图有关。

的ViewModels

public class UserGroupVM 
{ 
    public int Id { get; set; } 
    [Required] 
    [Display(Name = "Usergruppe")] 
    public string Name { get; set; } 
    [Display(Name = "Beschreibung")] 
    public string Description { get; set; } 
    [Required] 
    public Boolean IsGlobal { get; set; } 
    public List<Employee_UserGroupVM> Employees { get; set; } 
} 

public class Employee_UserGroupVM 
{ 
    public int Id { get; set; } 
    [Display(Name = "Kennung")] 
    public string SamAccountName { get; set; } 
    [Display(Name = "Name")] 
    public string LastnameFirstName { get; set; } 
    [Display(Name = "Admin")] 
    public bool IsAdmin { get; set; } 
} 

表 - 视图:

@model DAKCrmImport.Web.Models.UserGroupVM 

@{ 
    ViewBag.Title = "_UserGroup"; 
} 

@using (Html.BeginForm("Save", "UserGroup", FormMethod.Post, new { Id = "form-usergroup-add", novalidate = "false" })) 
{ 
    <fieldset> 
     <div class="container-form-clear"> 
      <a class='button form-clear' title='Formular leeren'>button clear</a> 
     </div> 
     @Html.HiddenFor(model => model.Id) 
     @Html.LabelFor(model => model.Name, new { @class = "label req" }) 
     @Html.TextBoxFor(model => model.Name, new { @class = "input"}) 
     @Html.LabelFor(model => model.Description, new { @class = "label" }) 
     @Html.TextAreaFor(model => model.Description, new { @class = "input", rows = "3", cols = "25" }) 
     <br /> 

     <a class='button form-add' title='MA-Berechtigung hinzufügen' id="bt-employee-add">button employee-add</a> 
     @for (int i = 0; i < Model.Employees.Count(); i++) 
     { 
      @Html.EditorFor(m => m.Employees[i]); 
     } 

     <input type="submit" name="submit" class="submit form-button" value="Speichern" id="bt-submit-usergroup" /> 
    </fieldset> 
} 

管窥:

@model DAKCrmImport.Web.Models.Employee_UserGroupVM 

@{ 
    ViewBag.Title = "_EmployeeList"; 
} 
<div class="div-employee-add"> 
    <div class="container-right"> 
     <a class="button form-remove-employee" title="MA entfernen" id="bt-employee-delete">button ma-remove</a> 
    </div> 
    <div class="container-left"> 
     @Html.LabelFor(model => model.SamAccountName) 
     @Html.TextBoxFor(model => model.SamAccountName, new { @class = "textbox-samaccountname" }) 
     @Html.TextBoxFor(model => model.LastnameFirstName, new { @class = "textbox-lastnamefirstname", disabled = "disabled" }) 
     @Html.LabelFor(model => model.IsAdmin) 
     @Html.CheckBoxFor(model => model.IsAdmin, new { @class = "checkbox-isadmin" }) 
    </div> 
</div> 

控制器:

public virtual ActionResult BlankEmployeeRow() 
{ 
    return PartialView("EditorTemplates/Employee_UserGroupVM", new Employee_UserGroupVM()); 
} 
public virtual ActionResult Save(UserGroupVM usergroup) 
{ 
    return null; 
} 

我到目前为止注意到的唯一事情是,如果我不使用局部视图并通过索引显示其变量,则现有项目Employees会出现在控制器中。因为我无法在局部视图中进行任何索引,所以现在不起作用。我不知道如何解决这个问题,无论是新的还是编辑过的项目都会出现问题。

我真的很感激一点帮助。

P.S.我知道如何通过jQuery读取模型,但我想使用MVC的本地表单提交方法,而不需要额外的代码。

[Update]我从部分视图中创建了一个EditorTemplate,现在已经提交了existring Employees,但是其他员工仍未提交。即使他们出现在标记...

通过jQuery增加新的项目/局部视图:

$(document).on('click', '#bt-employee-add', function() { 
    var lastDiv = $("[class~='div-employee-add']").first(); 
    var lastTextBox = $(lastDiv).find("[class~='textbox-lastnamefirstname']"); 

    var url = $("#EmployeeAdd").data('request-url'); 
    var params = null; 

    if (lastTextBox.val() == "") { 
     //Notification 
    } else { 
     $(getJSONfromController(url, params, null, true)).insertBefore($(".div-employee-add").first()); 
    } 
}); 
+1

首先将[HttpPost]属性添加到您的保存操作 –

+0

控制器的外观如何? – Niklas

+0

我在控制器中使用的两个操作已经在那里。我使用“保存”提交和BlankEmployeeRow添加一个空白的员工。 – OhSnap

回答

1

试试这个:

@Html.Partial("_EmployeeList", Model.Employees, new ViewDataDictionary 
{ 
    TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Employees" } 
}) 

的问题是部分不知道列表的名称,所以你必须明确指定它。

+0

这是正确的方式,但我没有解析列表的部分视图,但单个项目。它确实适用于EditorFor。至少只要我不引用部分视图“_EmployeeList”-.- – OhSnap

+0

好吧..使用EditorFor现在,但添加新项目不起作用。他们确实出现在标记中,但他们没有得到其他数据的提交......这真的很烦人 – OhSnap

+0

那么你是如何添加新项目的? jQuery的?你给他们正确的'name ='? –

0

当您使用简单的ViewModel时,您的数据不会动态更新,因为您的页面不会轮询您的服务器。它只会刷新新数据。

您将不得不使用计时器轮询或查看角度,挖空或余烬。