0

I have a Create view which contains a form. I want to give the user two options, save which simply saves the form and redirects to Index or add more tickets which will show another identical form in the view. I am unsure where to start with this, should I save the first ticket details then refresh the page? Or is there a way to have a second form hidden until add more tickets is selected then save all forms at once? I know this is quite vague but I've no idea where to start, but any advice would be great

Here is my model;

 public class Ticket
{
   public int TicketID { get; set; }

   [Required]
   [ForeignKey("Event")]
   //foreign key
    public int EventID { get; set; }

   [Required]
   public string Description { get; set; }

   [Required]
   public float Price { get; set; }

    //navigation property
    public virtual Event Event { get; set; }

    //navigation property
    public ICollection<OrderDetails> OrderDetails { get; set; }
}

Here is my view so far;

@model GeogSocSite.Models.Ticket

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Ticket</h4>
    <hr />
    @Html.HiddenFor(model => model.EventID)

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new {   @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes  = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new {  @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class =  "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new  { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
         </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

<div>
@Html.ActionLink("Back to List", "Index")
</div>
mara19
  • 121
  • 1
  • 4
  • 15
  • Adding another form would be pointless (you can only post one form at a time). Instead you can dynamically add new items to a collection as per the options in the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308). (your model would be a collection of `Ticket`) –  Nov 20 '15 at 21:35
  • @StephenMuecke thanks for your help again I'll take a look at the answers you've referenced – mara19 Nov 20 '15 at 21:53
  • Just realized your previous question. You could consider dynamically adding tickets in the event form and saving the event and its associated tickets all at once. –  Nov 20 '15 at 21:56
  • @StephenMuecke to add the tickets within the event form would I need a ViewModel which incorporates both the Event and the Ticket or because the Event model has a collection of Tickets is this unnecessary? – mara19 Nov 20 '15 at 22:39
  • A view model makes it easier and is recommended rather than using data models in your view especially when editing data (but its not essential) –  Nov 20 '15 at 22:43
  • @StephenMuecke thanks for your help, I will try to create a ViewModel and add the event and tickets all in one view – mara19 Nov 20 '15 at 22:58
  • Before you start developing your viewmodel, think about these things as well. _do you need to display total price_, _summary of total purchase, like number of tickets etc_. If so, you can add those summary fields to your viewModel and then (I may also think about a `OrderHeader` class, that contains all these summaries), add IEnumerable to your view model. Then you can create a view for your viewModel and **display templates** to display `Ticket` and `OrderDetails`. – Kosala W Nov 21 '15 at 00:06
  • @KosalaW at the minute i'm creating the admin side of my site, allowing admin to add events and then tickets for each event. Then I'm going to create the booking side of my site which will have an Order table and OrderDetails. I guess I'll need to try and make a ViewModel for this part of the site, thank you! – mara19 Nov 21 '15 at 16:17

1 Answers1

0

My choice in this situation is using appropriate view model(as @StephenMuecke ) sugested) and using Bootsrtaap modal(here) to show hidden fields.

Hadee
  • 1,392
  • 1
  • 14
  • 25