1

I'm working on a small "Rent-a-car" application. I have a list of Cars that client can see and i want to implement "Order feature" on that list. Every car should have order button on his side. When the client chooses car and presses button "Order" i want to have opened Bootstrap Modal that says something like "You want to order car that client selected , please enter your personal information". Then he will enter his name, email, and phone number. When he enters that he will press "Submit" button on that modal and he will get message something like "We sent a payment link and instruction for paying on your email. Please check your email." Then the client will get email that will say "You want to order car that the client selected . Please read following instructions: Some text"

I suppose i can do this with Action Links but i don't know how to implement it in my existing code

Please note: This doesn't have to be made using Bootstrap Modals. I am opened for your suggestions. Please review my existing code.

This is my Car model:

public class Car
{
    [Key]
    public int CarID { get; set; }
    public string Model { get; set; }
    [DisplayName("Year of production")]
    public int YearOfProduction { get; set; }
    public string Price { get; set; } 
    public virtual ICollection<FilePath> FilePaths { get; set; }
    [DisplayName("Air Conditioning")]
    public string AirConditioning { get; set; }
    [DisplayName("Engine")]
    public string EngineType { get; set; }
    public string Transmission { get; set; }
    public string Suitcases { get; set; }
    public string Seats { get; set; }
}

This is my Cars controller:

 public class CarsController : Controller
 {
    private CarContext db = new CarContext();

    // GET: Cars
    public ActionResult Index()
    {
        return View(db.Cars.ToList());
    }

    // GET: Cars/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        //Car car = db.Cars.Find(id);
        Car car = db.Cars.Include(i => i.FilePaths).SingleOrDefault(i => i.CarID == id);
        if (car == null)
        {
            return HttpNotFound();
        }
        return View(car);
    }

    // GET: Cars/Create
    [Authorize(Roles = "Administrator")]
    public ActionResult Create()
    {
        return View();
    }

    // POST: Cars/Create
    [Authorize(Roles = "Administrator")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "CarID,Model,YearOfProduction,Price,AirConditioning,EngineType,Transmission, Suitcases, Seats")] Car car, HttpPostedFileBase upload)
    {
        if (ModelState.IsValid)
        {
            if (upload != null && upload.ContentLength > 0)
            {
                var photo = new FilePath
                {
                    FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), //uniqueness of the file name
                    FileType = FileType.Photo
                };
                car.FilePaths = new List<FilePath>();
                upload.SaveAs(Path.Combine(Server.MapPath("~/Images/Cars"), photo.FileName));
                car.FilePaths.Add(photo);

            }
            db.Cars.Add(car);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(car);
    }



    // GET: Cars/Edit/5
    [Authorize(Roles = "Administrator")]
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Car car = db.Cars.Find(id);
        if (car == null)
        {
            return HttpNotFound();
        }
        return View(car);
    }


    [Authorize(Roles = "Administrator")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "CarID,Model,YearOfProduction,Price,AirConditioning,EngineType,Transmission, Suitcases, Seats")] Car car, HttpPostedFileBase upload)
    {
        if (ModelState.IsValid)
        {
            if (upload != null && upload.ContentLength > 0)
            {
                var photo = new FilePath
                {
                    FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), //uniqueness of the file name
                    FileType = FileType.Photo
                };
                car.FilePaths = new List<FilePath>();
                upload.SaveAs(Path.Combine(Server.MapPath("~/Images/Cars"), photo.FileName));
                car.FilePaths.Add(photo);

            }
            db.Cars.Add(car);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(car);
    }

    // GET: Cars/Delete/5
    [Authorize(Roles = "Administrator")]
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Car car = db.Cars.Find(id);
        if (car == null)
        {
            return HttpNotFound();
        }
        return View(car);
    }

    // POST: Cars/Delete/5
    [Authorize(Roles = "Administrator")]
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Car car = db.Cars.Find(id);
        db.Cars.Remove(car);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

This is my Index view of the Cars controller where i put list of the cars and modals for ordering:

@model IEnumerable<Testing_identity_2.Models.Car>

@{
ViewBag.Title = "Index";
}

<h2>AVAILABLE CARS</h2>

@if (ViewContext.HttpContext.User.IsInRole("Administrator"))
{
<p>
    @Html.ActionLink("Create New", "Create")
</p>
}

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Model)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Price)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td class="col-md-3">
            @Html.DisplayFor(modelItem => item.Model)
        </td>
        <td class="col-md-2">
            @Html.DisplayFor(modelItem => item.Price)
        </td>

        <td>

            <button class="btn btn-default btn-sm"  data-target="#orderModal" data-toggle="modal">Order</button>

            @Html.ActionLink("Details", "Details", new {id = item.CarID}, new {@class = "btn btn-default btn-sm"})

            @if (ViewContext.HttpContext.User.IsInRole("Administrator"))
            {
                @Html.ActionLink("Edit", "Edit", new {id = item.CarID}, new {@class = "btn btn-default btn-sm"})
            }

            @if (ViewContext.HttpContext.User.IsInRole("Administrator"))
            {
                @Html.ActionLink("Delete", "Delete", new {id = item.CarID}, new {@class = "btn btn-default btn-sm"})
            }

        </td>
    </tr>    
}

</table>

<div class="modal" data-keyboard="false" data-backdrop="static" id="orderModal" tabindex="-1">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title">Please enter your personal information</h4>
        </div>
        <div class="modal-body">
            <form>
                <div class="form-group">
                    <label for="inputName">Name</label>
                    <input class="form-control" placeholder="Name" type="text" id="inputName" />
                </div>
                <div class="form-group">
                    <label for="inputEmail">Email</label>
                    <input class="form-control" placeholder="Email"  type="text" id="inputEmail" />
                </div>
                <div class="form-group">
                    <label for="inputPhoneNumber">Phone Number</label>
                    <input class="form-control" placeholder="Phone Number" type="text" id="inputPhoneNumber" />
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button id="btnSubmitModal" class="btn  btn-primary">Submit</button>
            <button class="btn btn-primary" id="btnHideModal2">Close</button>
        </div>
    </div>
</div>
</div>


<div class="modal" data-keyboard="false" data-backdrop="static"  id="orderModal1" tabindex="-1">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">

        </div>
        <div class="modal-body">
            <form>
                <h4 class="modal-title">We sent a payment link on your email.</h4>
                <br />
                <h4 class="modal-title">Please check your email.</h4>
            </form>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary"  id="btnHideModal1">Close</button>
        </div>
    </div>
</div>
</div>


<script type="text/javascript">
$(document).ready(function() {

    $('#btnSubmitModal').click(function () {
        $('#orderModal').modal('hide');
    });

   $('#orderModal').on('hide.bs.modal', function (){
        $('#orderModal1').modal('show');
   });

   $('#btnHideModal1').click(function () {
       $('#orderModal1').modal('hide');
   });

    $('#btnHideModal2').click(function () {
        $('#orderModal').modal('hide');
        $('#orderModal1').modal('hide');
    });

    });
</script>
tereško
  • 58,060
  • 25
  • 98
  • 150
iantukic
  • 87
  • 2
  • 13

1 Answers1

0

There are several things that you are asking for. First, You need to add an actionlink tag with a binding id in your @foreach loop, for example:

@Html.Actionlink("Order car here!", "Order", new { id=item.CarID })

With the controller actionresult looking similar to this:

Public ActionResult Order(int id)
{
return View("Order");
}

Second, you want to have an order page in which the user can enter their data, using razor or angularJS. I recommend that you have a separate model for storing user data:

class client
{
 public int userID { get; set; }
 public string email { get; set; }
 public string Name { get; set; }
 public int PhoneNumber { get; set; }
}

An example Order page:

<div>
  <div>
    @Html.Label("emailaddress","Email address")
    @Html.Editor("Email")
  </div>
  <div>
    @Html.Label("NameLabel", "First Name")
    @Html.Editor("Name")
  </div>
  <div>
    @Html.Label("PhoneNumberLabel", "Phone Number")
    @Html.Editor("PhoneNumber")
  </div>
 @Html.ActionLink("Submit order form", "submitOrder", new { email=Model.email, name=Model.name, PhoneNumber=Model.PhoneNumber})
</div>

PLEASE NOTE: The order form is a rough outline and will need some work to submit the forms data. The form will then submit this data to another ActionResult that will utilise the SMTP namespace(System.Net.Mail):

public ActionResult subOrder(string email, string name, 
{
 MailMessage mail = new MailMessage("exampleNoReponseEmail@emailexample.com", email);
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.google.com";
mail.Subject = "Car payment conformation";
mail.Subject = "Dear, " + name + ", "You want to order car that the client selected . Please read following instructions: Some text";
client.Send(mail);
return View("ConfirmPayment");
}

The confirm email view:

<h1>Email Sent!</h1>
<p>We sent a payment link and instruction for paying on your email. Please check your email.</p>

These links about sending email might also help: 1. Send e-mail via SMTP using C#
2. Sending email in .NET through Gmail
3. https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx
4. https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx
I hope this helps!

Community
  • 1
  • 1
Boolean
  • 163
  • 12