我試圖動態地添加項目到列表中,並使這些項目顯示在我的控制器後提交,但現在我甚至沒有得到現有的列表 - 出現的項目。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());
}
});
首先將[HttpPost]屬性添加到您的保存操作 –
控制器的外觀如何? – Niklas
我在控制器中使用的兩個操作已經在那裏。我使用「保存」提交和BlankEmployeeRow添加一個空白的員工。 – OhSnap