Itsumo Kokoro
Itsumo Kokoro

Reputation: 169

My model is not binding during submit

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

enter image description here

Upvotes: 0

Views: 1469

Answers (1)

Rasik
Rasik

Reputation: 2410

You do not have the name on your attribute Html. So, while you post the form to controller, the value does not bind to the property on the model.

Change your loop for ameniety with the code as below.

<tbody>
@for (int i = 0; i < Model.AddAmenity.Count; i++)
{
  <tr>

    <td>@Html.HiddenFor(m => m.AddAmenity[i].Amenity_id)</td>
    <td>@Html.TextBoxFor(m => m.AddAmenity[i].Amenity_Name, new { @readonly = "readonly" })</td>
    <td>@Html.TextBoxFor(m => m.AddAmenity[i].Amenity_Price, new { @readonly = "readonly" })</td>
    <td>@Html.TextBoxFor(m => m.AddAmenity[i].Quantity)</td>
  </tr>
}
</tbody>

If you also wanted to submit the value from Room_id, Room_Type and Room_rate. Either use the

@Html.EditorFor(m => m.Room_id)

Or

@Html.HiddenFor(m => m.Room_id) 
@Html.DisplayFor(m => m.Room_id)

So on, for type and rate.

You need to use HiddenFor and DisplayFor because

HiddenFor is used to have the values ready to be posted, and DisplayFor to show those values.

Refer:

  1. MVC4 Razor - @Html.DisplayFor not binding to model

  2. Post an HTML Table to ADO.NET DataTable

Upvotes: 1

Related Questions