I'm practicing MVC ASP.net and I am having a problem. During load, I get the data and passed it to model for display and try to pass it again for posting to save the data but then the model is not getting the data.Please help me for what I am missing. Thank you
I attached an image, what does it display and those data should get on posting.
My Model
public class RoomReservation
{
public RoomReservation()
{
AddAmenity = new List<AmenityReservation>();
}
public int Room_id { get; set; }
public string Room_name { get; set; }
public int RoomType_id { get; set; }
public string Room_Type { get; set; }
public decimal Room_rate { get; set; }
public List<AmenityReservation> AddAmenity { get; set; }
}
public class AmenityReservation
{
public int Amenity_id { get; set; }
public string Amenity_Name { get; set; }
public decimal? Amenity_Price { get; set; }
public int Quantity { get; set; }
}
}
My Controller
public ActionResult RoomReservation(int RoomID)
{
RoomReservationEntities db = new RoomReservationEntities();
var model = new RoomReservation();
model.Room_id = RoomID;
model.Room_name = db.Rooms.Where(x => x.Room_id == RoomID).Select(r => r.Room_name).FirstOrDefault();
model.RoomType_id = db.Rooms.Where(x => x.Room_id == RoomID).Select(r => r.Room_Type.RoomType_id).FirstOrDefault();
model.Room_Type = db.Rooms.Where(x => x.Room_id == RoomID).Select(r => r.Room_Type.RoomType).FirstOrDefault();
model.Room_rate = db.Rooms.Where(x => x.Room_id == RoomID).Select(r => r.Room_Type.Room_rate).FirstOrDefault();
var amenities = db.Amenities.ToList();
foreach (var n in amenities)
{
model.AddAmenity.Add(new AmenityReservation { Amenity_id = n.Amenity_id, Amenity_Name = n.Amenity1, Amenity_Price = n.Price, Quantity = 0 });
}
return View(model);
}
[HttpPost]
public ActionResult RoomReservation(RoomReservation model)
{
//here the model is not binding from display
return View();
}
My View
@model Reservation_System.Models.RoomReservation
@{
ViewBag.Title = "RoomReservation";
Layout = "~/Views/Shared/_MasterLayout.cshtml";
}
<div style="padding-top:10%">
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<section class="container tm-home-section-1" id="more">
<div class="row">
<div class="col-12">
<div class="col-lg-6 col-md-6">
<div class="jumbotron">
<h2>RoomReservation</h2>
<div class="row">
@Html.DisplayFor(x => x.Room_id)
</div>
<div class="row">
@Html.DisplayFor(x => x.Room_Type)
</div>
<div class="row">
@Html.DisplayFor(x => x.Room_rate)
</div>
</div>
</div>
<div class="col-lg-6 col-md-6">
<table class="table">
<thead>
<tr>
<td style="visibility:hidden"></td>
<td>Amenity</td>
<td>Price</td>
<td>Quantity</td>
</tr>
</thead>
<tbody>
@foreach (var n in Model.AddAmenity)
{
<tr>
<td style="visibility:hidden">@n.Amenity_id</td>
<td>@n.Amenity_Name</td>
<td>@n.Amenity_Price</td>
<td><input type="text" value="@n.Quantity" /> </td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
<div class="row">
<div>
<input type="submit" value="Reserve" />
</div>
</div>
</section>
}
Display in view and data should pass during posting for saving in the database