Create MVC with Multiple Enity Updates
Create a MVC Application and add 2 enities
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public string PhoneNo { get; set; }
public List<Department> Departments { get; set; }
}
public class Department
{
public string Code{ get; set; }
public string Name { get; set; }
}
Add Controller : TestContoller
public class TestController : Controller
{
// GET: Test
public ActionResult Create()
{
return View(new Employee());
}
[HttpPost]
public ActionResult Create(Employee emp, List<Department> depts)
{
emp.Departments = depts;
return View(emp);
}
}
Add View for empty Create action method :
Add below code for multiple textbox departments
<table>
<tr>
<th>Code</th>
<th>Name</th>
</tr>
@{
for (int i = 0; i < 4; i++)
{
<tr>
<td> @Html.Editor("[" + i + "].Code") </td>
<td> @Html.Editor("[" + i + "].Name")</td>
</tr>
}
}
</table>
Below code display output :
<div>
@{
if (Model.Name != null)
{
<div>
Emp Name : @Model.Name <br />
Location : @Model.Location<br />
PhoneNo : @Model.PhoneNo <br />
@{
if (Model.Departments != null)
{
<table>
<tr>
<th>code</th>
<th>Name</th>
</tr>
@{
foreach (var dept in Model.Departments)
{
<tr>
<td>@dept.Code</td>
<td>@dept.Name</td>
</tr>
}
}
</table>
}
}
</div>
}
}
</div>
output :
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.