-1

Currently I've got this:

AddEvent.cshtml

    @model Evenementor.Models.EvenementorEvent
    @{
        ViewBag.Title = "Voeg een evenement toe";
    }

    @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
                {
                    <div class="12u">
                        @Html.TextBoxFor(m => m.Title, new { @placeholder = "Titel" })
                    </div>
                    <div class="12u">
                        @Html.TextAreaFor(m => m.Description, new { @placeholder = "Omschrijving" })
                    </div>
                    <div class="6u">
                        @Html.TextBoxFor(m => m.Startdate, new { @placeholder = "Startdag" })
                    </div>
                    <div class="6u">
                        @Html.TextBoxFor(m => m.Enddate, new { @placeholder = "Einddag" })
                    </div>
                    <div class="12u">
                        @Html.TextBoxFor(m => m.Location, new { @placeholder = "Locatie" })
                    </div>
                    <div class="12u">
                        <select name="Type">
                            @foreach (var item in ViewData["Types"] as List<Evenementor.Models.EvenementorType>)
                        {
                            <option value="@item.TypeID">@item.Name</option>
                        }
                        </select>
                    </div>
                <input type="submit" value="Voeg toe" />
                }

DashboardController.cs

        // GET: /Dashboard/AddEvent
        public ActionResult AddEvent()
        {
            if (!User.Identity.IsAuthenticated)
            {
                // User isn't allowed here
                Response.Redirect("/");
            }
            ViewData["Types"] = EvenementorType.GetAllTypes();
            return View();
        }



        // POST: /Dashboard/AddEvent
        [HttpPost]
        public ActionResult AddEvent(EvenementorEvent ev)
        {
            if (!User.Identity.IsAuthenticated)
            {
                // User isn't allowed here
                Response.Redirect("/");
            }
            if (ModelState.IsValid)
            {
                EvenementorEvent e = new EvenementorEvent();
                e.Title = ev.Title;
                e.Description = ev.Description;
                e.Startdate = ev.Startdate;
                e.Enddate = ev.Enddate;
                e.Location = ev.Location;
                // Register the event
                e.Register(e);
                // Make a link between event and organisation
                EvenementorOrganisationsEvent l = new EvenementorOrganisationsEvent();
                l.EventID = e.EventID;
                l.OrganisationID = EvenementorOrganisation.GetOrganisationByEmail(User.Identity.Name).OrganisationID;
                l.Register(l);
                // Link between event and type
                EvenementorEventsType ty = new EvenementorEventsType();
                ty.EventID = e.EventID;
//GET THE OTHER INFO (Types)
                // Redirect
                Response.Redirect("/Dashboard/");
            }

            // Something's wrong!
            return View(ev);
        }

How can I get the data from the input from the dropdown back into my controller? Or is there any other/better solution to this?

Thanks in advance, I'm a total newb to ASP.NET. Still learning on my own.

user3581249
  • 39
  • 2
  • 6
  • You might want to read up on using the [AuthoriseAttribute](http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx) –  Sep 02 '14 at 23:38

2 Answers2

1

You have 2 options with the code provided.

Add a Type property to your EvenementorEvent class so it will bind to it,

public class EvenmentorEvent {
   public int Type { get;set; }
}

Or .. Add a parameter to your action:

public ActionResult AddEvent(EvenementorEvent ev, int type)

The people below put much more effort into their answers and indeed do give you the more correct "MVC" way to do things. If you have the time to take in their examples and implement them, they might work out better for you. Depending on your requirements.

Chad Grant
  • 44,326
  • 9
  • 65
  • 80
  • Both options give the error: Object reference not set to an instance of an object. on the foreach in my View. – user3581249 Sep 02 '14 at 23:37
  • Yes, because this: ViewData["Types"] = EvenementorType.GetAllTypes(); needs to be added before your //Something went wrong! return View(ev); – Chad Grant Sep 02 '14 at 23:39
  • Indeed. But now I just get a "0" back. I need an int > 0, the value from the dropdown back. – user3581249 Sep 02 '14 at 23:44
  • I've gone with your option of adding a parameter and it works now. The other option just gives a "0" back. I'll mark this as the answer. Thanks! – user3581249 Sep 02 '14 at 23:50
  • You could also change the type from a string to an int, mvc will bind that as well – Chad Grant Sep 03 '14 at 00:24
1

ViewData is only used for one way trip ( controller -> View ). If you want to pass the selected value from the dropdown to your controller, you should create a new property in your model and bind it strongly in your view.

Model ->

public class EvenementorEvent
{
  public string Title { get;set;}
  public string Description { get;set;}
  ....
  ....
  public IEnumerable<System.Web.Mvc.SelectListItem> ListOfData { get;set;}
  public string SelectedData { get;set;}
}

Controller ->

    public ActionResult Index()
    { 
      EvenementorEvent ev = new EvenementorEvent();
      .............
      .............
      ev.ListOfData = ( Populate Data - Transform your collection to Collection of 

System.Web.Mvc.SelectListItem) 

      return View(ev);
    }

    [HttpPost]
    public ActionResult AddEvent(EvenementorEvent ev)
    {
      ev.SelectedData  -> The selected value from the dropdown
    }

View ->

@Html.DropDownListFor(m => m.SelectedData, Model.ListOfData , new { id = "dropdown" })

Make changes are per your requirement. Hope it helps.

DinoMyte
  • 8,737
  • 1
  • 19
  • 26