2016-11-13 7 views
0

私は小さな「レンタカー」アプリケーションで作業しています。クライアントが見ることができる車のリストがあり、そのリストに「注文機能」を実装したいと思っています。すべての車は、彼の側に注文ボタンを持っている必要があります。クライアントが車を選んで「オーダー」ボタンを押すと、「あなたが車を選択して、車を注文したい、あなたの個人情報を入力してください」といった何かを言うBootstrap Modalを開きたいと思う。それから彼は彼の名前、電子メール、および電話番号を入力します。彼が入力すると、彼はそのモーダルで「送信」ボタンを押して、「あなたのメールで支払うための支払いリンクと指示を送った」というようなメッセージを受け取ります。 "BootstrapモーダルでActionLink経由で電子メールを送信

私はアクションリンクでこれを行うことができますが、私はどのようにわかっているのでしょうか?あなたは何を言っているのですか?既存のコードに実装するには

注意:これはブートストラップモードを使用して行う必要はありません。私はあなたの提案のために開かれています。既存のコードを確認してください。

これは私の車のモデルである:

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; } 
} 

これは私の車のコントローラである:

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); 
    } 
} 

これは私が注文するため車やモーダルのリストを入れて車コントローラの私のIndexビューであります:

@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> 

答えて

0

あなたが求めていることがいくつかあります。まず、次の例のように、あなたの@foreachループに結合IDでActionLinkのタグを追加する必要があります。

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

コントローラではこれに類似探しのActionResult:

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

第二に、あなたがしたいですユーザーがrazorまたはangularJSを使用してデータを入力できる注文ページ。私は、ユーザーデータを格納するためにあなたが別のモデルを持っていることをお勧めします。

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

例Orderページ:注文フォームが粗い輪郭線で、提出するいくつかの作業が必要になります。

<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> 

ご注意データを形成する。

public ActionResult subOrder(string email, string name, 
{ 
MailMessage mail = new MailMessage("[email protected]", 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"); 
} 

確認メールビュー:も役立つかもしれない電子メールの送信について

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

これらのリンク: 1の形式は、SMTP名前空間を利用する別のActionResultに、このデータ(System.Net.Mail)を提出します。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
私はこれが助けてくれることを願っています!

関連する問題