I'm a newbie to MVC5. I want to add building details to database. The building details include name, list of room details, etc.. The user can add building details such as name, and list of room details. After adding all details the list of room and building details are saved to database.
I have form with a view-model as below
BuildingViewModel.cs
public class Building
{
public Building()
{
Rooms = new List<Room>();
NewRoom = new Room();
}
[Required]
public string Name { get; set; }
public List<Room> Rooms { get; set; }
public Room NewRoom { get; set; }
public string Button { get; set; }
}
public class Room
{
[Required]
public string RoomName { get; set; }
public string Area { get; set; }
}
This is my view Create.cshtml
@model Building
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
<h2>Create</h2>
<h3>Building</h3>
@Html.LabelFor(t => t.Name)
@Html.TextBoxFor(t => t.Name)
@Html.ValidationMessageFor(t => t.Name)
<br />
<br />
<fieldset>
<legend>Room</legend>
<div class="editor-label">
@Html.LabelFor(model => model.NewRoom.RoomName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NewRoom.RoomName)
@Html.ValidationMessageFor(model => model.NewRoom.RoomName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NewRoom.Area)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NewRoom.Area)
@Html.ValidationMessageFor(model => model.NewRoom.Area)
</div>
<p>
<input type="submit" name="button" value="Add Room" />
</p>
</fieldset>
<br />
<table border="1" cellpadding="5px" cellspacing="0">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Rooms.Count(); i++)
{
var room = Model.Rooms[i];
<tr>
<td>@(i + 1)</td>
<td>@room.RoomName @Html.HiddenFor(t => room.RoomName)</td>
<td>@room.Area @Html.HiddenFor(t => room.Area)</td>
</tr>
}
</tbody>
</table>
<br />
<input type="submit" name="button" value="Create" />
}
<script src="~/Scripts/jquery-1.8.0.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
And this is my controller BuildingController.cs
public class BuildingController : Controller
{
[HttpGet]
public ActionResult Create()
{
var building = new Building();
return View(building);
}
[HttpPost]
public ActionResult Create(Building building)
{
switch (building.Button)
{
case "Add Room":
if (ModelState.IsValid)
{
building.Rooms.Add(building.NewRoom);
building.NewRoom = new Room();
return View(building);
}
break;
case "Create":
if (ModelState.IsValid)
{
// TODO: save to db
return View();
}
break;
}
return View(building);
}
}
My problem is that when 'Add Room' button is clicked, it throws a validation error in Name property of building. What i want is, show validation only for Room class properties when 'Add Room' button is clicked, and show validation error for Building property when 'Create' button is clicked.
Add Building Validation Error Image
I'm spending more than 2 week for this problem. Please help me...
Thanks for your valuable time.