【发布时间】:2015-04-21 14:16:31
【问题描述】:
我在下面对问题进行了更改,它仍然是相同的,但希望通过模型以及我想要实现的目标以及我遇到的问题更加清晰。
下面显示了两个类,Company 和 Employee,Company 有一个Employees 列表。
这将是一个输入表单,因此一开始就没有数据。
最终,我希望用户能够根据需要向 Company 对象模型中添加任意数量的 Employee 对象并更新 Employee 对象
我是否在使用 BeginCollectionItem 的正确轨道上,以便可以添加/删除任意数量的 Employee 对象? 当我单击“添加”按钮时,它会将其带到另一个页面上的部分视图(使用 AjaxActionLink),但不使用 JavaScript。
更新 删除了 AjaxActionLink 并改用 JavaScript。
索引
@model MvcTest.Models.Company
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Company</h2>
<div>
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
</div>
<fieldset>
<legend>Employees</legend>
<div id="new-Employee">
@foreach (var Employee in Model.Employees)
{
Html.RenderPartial("_Employee", Employee);
}
</div>
<div>
<input type="button" id="addemployee" name="addemployee" value="Add Employee"/>
<br/>
</div>
<br/>
@section Scripts
{
<script type="text/javascript">
$('#addemployee').on('click', function () {
$.ajax({
async: false,
url: '/Company/AddNewEmployee'
}).success(function (partialView) {
$('#new-Employee').append(partialView);
});
});
</script>
}
</fieldset>
<div>
<input type="submit" value="Submit" />
</div>
_Employee PartialView
@model MvcTest.Models.Employee
@using (Html.BeginCollectionItem("Employees"))
{
<div class="employeeRow">
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
@Html.LabelFor(m => m.Telephone)
@Html.EditorFor(m => m.Telephone)
@Html.LabelFor(m => m.Mobile)
@Html.EditorFor(m => m.Mobile)
@Html.LabelFor(m => m.JobTitle)
@Html.EditorFor(m => m.JobTitle)
<a href="#" class="deleteRow">Delete</a>
</div>
}
@section Scripts
{
$("a.deleteRow").live("click", function(){
$(this).parents("div.employeeRow:first").remove();
return false;
});
}
控制器
public class CompanyController : Controller
{
// GET: Company
public ActionResult Index()
{
var newCompany = new Company();
return View(newCompany);
}
public ActionResult AddNewEmployee()
{
var employee = new Employee();
return PartialView("_Employee", employee);
}
}
型号
public class Company
{
[Key]
public int Id { get; set; }
[Display(Name = "Company")]
public string Name { get; set; }
public List<Employee> Employees { get; set; }
//public Company()
//{
// Employees = new List<Employee>
// {
// new Employee{ Name = "Enter name"}
// };
//}
}
public class Employee
{
[Key]
public int Id { get; set; }
[Display(Name="Employee")]
public string Name { get; set; }
public string Telephone { get; set; }
public string Mobile {get;set;}
[Display(Name="Job Title")]
public string JobTitle {get;set;}
}
【问题讨论】:
-
我想你想动态添加/删除classb中的项目:github.com/danludwig/BeginCollectionItem
-
这听起来很适合使用 ajax :)
-
我目前正在研究 AJAX 与它的使用,以前从未使用过 AJAX,我看过有关 BeginCollectionItem 的帖子但没有完全理解它,动态不是现在的首要任务.
-
@SelrekJohn 我在回答中使用 BeginCollectionItem 添加了一个基本结构。你将不得不清理它
-
如果您的部分视图显示在另一个页面上,那么您没有包含
jquery.unobtrusive-ajax.js文件(或者您的脚本顺序错误,或者有重复的脚本)。您也有UpdateTargetId = "Employees",但似乎没有带有id="Employees"的元素(但有一个<div class="new-course">,看起来像您要渲染部分的位置?)。在任何情况下,摆脱Ajax.ActionLink()并改用jquery。
标签: c# asp.net-mvc razor