我在下面对这个问题做了一些修改,这个问题仍然是一样的,但希望通过模型和关于我想要实现的目标以及我在哪里遇到的问题能够更清楚地了解这个问题。
下面是两个类,公司和员工,公司有员工名单。
这将是一个输入表单,因此将没有数据在那里开始。
最终,我希望用户能够将尽可能多的Employee对象添加到公司对象模型中,并对Employee对象进行更新
使用BeginCollectionItem是否正确,这样我就可以添加/删除任意数量的Employee对象了吗?当我单击Add按钮时,它会将它带到另一个页面上的部分视图(使用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;}
}发布于 2015-04-25 18:55:39
为了实现这一点,您不需要使用BeginCollectionItem。从必须亲自查看它并尝试将其用于类似的问题,它似乎是针对早期版本的MVC创建的这种性质的问题。
使用部分视图显示和更新列表。一个部分视图显示并遍历对象列表,另一个视图创建一个新对象,在回发以更新列表时,该对象将在该列表的分部视图中显示新创建的对象。
我在这里发布了一个类似的问题,应该解决你的问题,click here
希望这能有所帮助。
更新删除不能工作的原因是无法从Partial调用JS,将其放在主视图(@section )中。另外,我认为你在div中的类和id关键字有点混乱,下面看一看。
所以你应该:
局部视图
@model MvcTest.Models.Employee
@using (Html.BeginCollectionItem("Employees"))
{
<div id="employeeRow" 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="#" id="deleteRow" class="deleteRow" onclick="deleteFunction()">Delete</a>
</div>
}主视图
@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);
});
});
$("#deleteRow").live("click", function () {
$(this).parents("#employeeRow:first").remove();
return false;
});
</script>
}
</fieldset>
<div>
<input type="submit" value="Submit" />
</div>发布于 2015-04-21 14:59:27
让我们假设ClassA是您的ViewModel:
只需要一个部分视图或视图就可以同时更新两个视图:
例如Edit.cshtml
@model ClassA
@Html.BeginForm(){
@Html.TextBoxFor(m => m.name)
for(int i = 0; i< Model.classB.Count; i++){
@Html.TextBoxFor(m => Model.classB[i].name)
<button type="button"> Add </button>
<button type="button"> Remove </button>
}
<input type="submit" value = "save"/>
}注意:在您的部分视图中,您使用的是foreach循环,MVC Model要求输入字段的格式为:
list[0].prop1
list[0].prop2
list[0].prop3
list[1].prop1
list[1].prop2
list[1].prop3因此,我们使用for循环
然后在控制器中:
[HttpPost]
public ActionResult Edit(ClassA model){
// HEre you will see model.ClassB list
// you can then save them one by one
foreach(var item in Model.classB){
save
}
return View(model);
}如果要动态添加或删除列表中的项,请执行以下操作:
创建另一个部分视图classBs
@model List<classB>
<div id="lists">
foreach (var contact in Model)
{
@Html.Partial("ClassBRow", contact)
}
</div>
<button data-action-url="@Url.Action("ClassBRow","CONTROLLER")" class="btn btn-primary pull-right" id="addItem">Add</button>
<script>
$("#addItem").click(function () {
var btn = $(this);
$.ajax({
url: btn.data('action-url'),
success: function (html) {
$("#lists").append(html);
}
});
return false;
});
</script>创建另一个部分视图:ClassBRow.cshtml
@model classB
using (Html.BeginCollectionItem("classB"))
{
@Html.HiddenFor(m => m.isDeleted, new { data_is_deleted = "false" })
@Html.TextBoxFor(m => Model.name, new { @class = "form-control" })
<span class="glyphicon glyphicon-trash" data-action="removeItem" title="remove" style="cursor:pointer"></span>
}在您的控制器中:
public ActionResult ClassBRow()
{
return PartialView(new classB());
}Edit.cshtml变成:
@model ClassA
@Html.BeginForm(){
@Html.TextBoxFor(m => m.name)
@Html.Partial("classBs", model.classB)
<input type="submit" value = "save"/>
}发布于 2015-04-21 14:37:19
把这些观点看作是独立的。如果部分视图是完整视图,则将传递给它一个IEnumerable<ClassB>。
因此,应:
@Html.Partial("ClassBView", Model.ClassB)此外,您不能将foreach与EditorFor一起使用,因为没有足够的信息来创建基于索引的名称。使用普通的for循环代替。表达式解析器随后可以将其转换为name="name[1]"等,因为该索引在表达式中可用:
@model IEnumerable<Project.Models.ClassB>
@if(Model != null)
{
for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.EditorFor(modelItem => Model[i].Name)
<input type="button" value="Clear" />
<input type="submit" value="Create" />
</td>
</tr>
}
}您的示例中缺少更多的内容(例如,Clear和Create连接到的内容),因此如果您能够提供其余的控制器代码,我将对其进行扩展。
https://stackoverflow.com/questions/29774822
复制相似问题