Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
controller --

<pre>namespace BusReservation.Controllers
{
    //public class Bus
    //{
    //    public string ID { get; set; }
    //    //public string City { get; set; }
    //    //public string Address { get; set; }

    //}


    public class BuSSeatDetail
    {
        public int SeatID { get; set; }
        public int SeatNo { get; set; }
    }
    public class BusController : Controller
    {
        private SqlConnection con;    

        // GET: Bus
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(BuSSeatDetail obj1)
        {
            AddDetails(obj1);
            //using (DashboardOrderEntities Obj = new DashboardOrderEntities())
            //{
            //    //Obj.Bs.Add(ID);
            //    //Obj.SaveChanges();
            //    //return " Successfully";

            //}
            return View();
            //return Json(ID);

        }
        private void connection()
        {
            string constr = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
            con = new SqlConnection(constr);

        }
        private void AddDetails(BuSSeatDetail obj1)
        {
            connection();
            SqlCommand com = new SqlCommand("Add", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@SeatNo", obj1.SeatNo);
            //com.Parameters.AddWithValue("@City", obj.City);
            //com.Parameters.AddWithValue("@Address", obj.Address);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();

        }    
    }
}



jquery -


$('#Save1').click(function () {
        var SeatNo = [], item;
        $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
            item = $(this).attr('title');
            SeatNo.push(item);
        });
        alert(SeatNo.join(','));
        $.ajax({
            //type: "POST", dataType: 'JSON',

            //url: "/Bus/Index",
            //data: JSON.stringify(str),
            ////contentType: "application/json; charset=utf-8",
            ////dataType: "json",
            //success: function (r) {
            //    ////data: JSON.stringify(item),
            //    ////$("#lblName").html(str.join(','));
            //    //alert(str.join(','));
            //    alert(r + " record(s) inserted.");


            type: "POST",
            url: "/Bus/Index",
            data: JSON.stringify(SeatNo),
            //data: {'bookseats': bookseats},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                alert(r + " record(s) inserted.");
            }

        });
    })




css ---

#holder {
    height: 200px;
    width: 550px;
    background-color: #F5F5F5;
    border: 1px solid #A4A4A4;
    margin-left: 10px;
}

#place {
    position: relative;
    margin: 7px;
}

    #place a {
        font-size: 0.6em;
    }

    #place li {
        list-style: none outside none;
        position: absolute;
    }

        #place li:hover {
            background-color: yellow;
        }

    #place .seat {
        background: url("../images/available_seat_img.gif") no-repeat scroll 0 0 transparent;
        height: 33px;
        width: 33px;
        display: block;
    }

    #place .selectedSeat {
        background-image: url("../images/booked_seat_img.gif");
    }

    #place .selectingSeat {
        background-image: url("../images/selected_seat_img.gif");
    }

    #place .row-3, #place .row-4 {
        margin-top: 10px;
    }

#seatDescription li {
    verticle-align: middle;
    list-style: none outside none;
    padding-left: 35px;
    height: 35px;
    float: left;
}


----

view -

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    @*<meta charset="utf-8">
        <meta http-eqiuv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">*@
    <title>Seat Booking</title>
    @*<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css" />
        <link rel="stylesheet" type="text/css" href="css/seatBooking.css" />
        <link rel="stylesheet" type="text/css" href="css/style.css" />*@

    @Styles.Render("~/css/bootstrap.min.css")
    @Styles.Render("~/css/bootstrap-theme.min.css")
    @Styles.Render("~/css/seatBooking.css")
    @Styles.Render("~/css/style.css")

    @Scripts.Render("~/bundles/modernizr")

    @*<link rel="shortcut icon" type="image" href="images/available_seat_img.gif">*@

    <link rel="icon" href="@Url.Content("~/images/available_seat_img.gif")" />


</head>
<body>
    <div id="container">
        <nav role="navigation" class="navbar navbar-default navbar-fixed-top">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                        Toggle navigation
                        
                        
                        
                    </button>
                    <a href="#" class="navbar-brand">Brand</a>
                </div>
                <!-- Collection of nav links and other content for toggling -->
                <div id="navbarCollapse" class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="home.html">Home</a></li>
                        <li><a href="#">Profile</a></li>
                        <li><a href="#">Messages</a></li>
                    </ul>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#">Login</a></li>
                    </ul>
                </div>
            </div>
        </nav>
        <div class="row">
            <h2> Choose seats by clicking the corresponding seat in the layout below:</h2>
            <div class="col-lg-6 col-sm-4" id="holder">
                <ul id="place"></ul>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-6 col-sm-4">
                <ul id="seatDescription">
                    <li style="background:url('images/available_seat_img.gif') no-repeat scroll 0 0 transparent;">Available Seat</li>
                    <li style="background:url('images/booked_seat_img.gif') no-repeat scroll 0 0 transparent;">Booked Seat</li>
                    <li style="background:url('images/selected_seat_img.gif') no-repeat scroll 0 0 transparent;">Selected Seat</li>
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-6 col-sm-4">
                <a href="#" id="btnShowNew" class="btn btn-primary btn-lg btn-success btn-fresh">Show Selected Seats" </a>
                <a href="#" id="btnShow" class="btn btn-primary btn-lg btn-success btn-fresh">Show All</a>
                @Html.Label("Name", new { @id = "lblName" })
                @*<input type="hidden" value="" name="bookseats" />*@
                <a href="#" id="Save" >Save</a>
                <a href="#" id="Save1">Save1</a>

            </div>

        </div>

    </div>
    <div class="content">
        @RenderBody()
    </div>

    @*<script src="lib/angular.min.js"></script>
        <script src="js/jquery-2.1.1.min.js"></script>
        <script src="js/seatBooking.js"></script>*@

    @Scripts.Render("~/lib/angular.min.js")
    @Scripts.Render("~/js/jquery-2.1.1.min.js")
    @Scripts.Render("~/js/seatBooking.js")
    @RenderSection("scripts", required: false)
</body>
</html>




database -

alter procedure [dbo].[Add]    
(    
    
   @SeatNo int
)    
as    
begin    
   Insert into [BuSSeatDetails] values( @SeatNo)    
End   


select * from  [BuSSeatDetails]

CREATE TABLE [dbo].[BuSSeatDetails](
	
	[SeatID] [int] primary key identity(1,1) ,SeatNo int Not Null) 


What I have tried:

getting null values in controller ( not able to save in db)
Posted
Updated 8-Jul-21 6:41am
v8
Comments
Richard Deeming 8-Jul-21 9:26am    
Removing the content of your question after others have taken the time to try to help you is extremely rude.

I have rolled back your destructive edit.
Richard MacCutchan 8-Jul-21 12:47pm    
He's still trying (very).

1 solution

Quote:
C#
public class BuSSeatDetail
{
    public int SeatID { get; set; }
    public int SeatNo { get; set; }
}
Your model accepts a single integer called SeatNo.
Quote:
JavaScript
var SeatNo = [], item;
$.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
    item = $(this).attr('title');
    SeatNo.push(item);
});
$.ajax({
    type: "POST",
    url: "/Bus/Index",
    data: JSON.stringify(SeatNo),
Your Javascript is posting an array of values to the controller.

We can't see the code that populates the place list. But even assuming that the title of every <li> in that list is a number, you cannot store an array of numbers in a single number.

You need to change your client-side code so that it posts data in the format expected by the server. Or change your server-side code so that it accepts data in the format sent by the client.
 
Share this answer
 
Comments
Maciej Los 9-Jul-21 2:39am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900