Hej udviklere, jeg sidder og arbejder på en ASP.NET applikation som overskriften lyder. Jeg forsøger at indsætte en værdi som bliver valgt fra en ud af to dropdownlister, værdien som kommer fra en af de to droplister skal gerne indsættes i databasen.
Det drejer sig om denne side:
http://beta.scoutpocket.dk/opret-spiller, bare lige for at give et visuelt billede af hvad det er jeg snakker om.
Ideen er at brugeren vælger hvilken serie vedkommenede er en del af ved at vælge en valgmuliged ved at klikke på en af radio knapperne.
For at gøre det lettere vedhæfter jeg alt det kode, som jeg bruger.
View:
- @model Scoutpocket.ViewModels.PlayerProfileViewModel
-
- @{
- ViewBag.Title = "Opret spiller";
- }
-
- <script src="~/Scripts/jquery-1.10.2.js"></script>
- <script src="~/Scripts/jquery.validate.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
-
- <script type="text/javascript">
-
-
-
- function validateDays() {
- if (document.getElementById("option1").checked == true) {
-
-
- var hiddenDiv = document.getElementById("Man");
- hiddenDiv.style.display = (this.value == "") ? "none" : "block";
-
- document.getElementById('Women').style.display = 'none';
-
- //bliver sat til 0 indexet i dropdownmenuen
- $("#ddlWomen")[0].selectedIndex = 0;
-
- }
- else if (document.getElementById("option2").checked == true) {
-
-
- var hiddenDiv = document.getElementById("Women");
- hiddenDiv.style.display = (this.value == "") ? "none" : "block";
-
-
- document.getElementById('Man').style.display = 'none';
-
- //bliver sat til 0 indexet i dropdownmenuen
- $("#ddlMan")[0].selectedIndex = 0;
- }
-
- else {
- // DO NOTHING
- }
- }
-
- </script>
-
-
-
-
-
- <style>
- .football-field {
- background-image: url(../../Content/Img/2050x925/Add-player.png);
- background-size: cover;
- position: relative;
- height: auto;
- padding: 0;
- margin: 0;
- }
-
- .errorMessage {
- display: none;
- color: red;
- }
-
- .btn.btn-info {
- background-color: #f9c63b;
- padding: 0px;
- height: 36px;
- font-size: 15px;
- border: none;
- -o-transition: all 0.218s;
- -moz-transition: all 0.218s;
- -webkit-transition: all 0.218s;
- transition: all 0.218s;
- }
-
- .btn.btn-info:hover,
- .btn.btn-signin:active,
- .btn.btn-signin:focus {
- background-color: #d39d08;
- }
- </style>
-
-
-
- <div class="jumbotron football-field">
- <div class="container" style="min-height:92vh;">
-
- <div class="col-md-4"></div>
-
- <div class="col-md-4">
-
- @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "formID" }))
- {
-
-
- @Html.AntiForgeryToken()
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
-
- <div class="form-group" style="margin-top:35px; margin-bottom:20px;">
- <h2 style="color:white; text-align:center; font-family: Arial Narrow; font-size:30pt">OPRET SPILLER</h2>
- </div>
- <div class="form-group">
- @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Fornavn", required = "required" } })
- @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
-
- </div>
-
- <div class="form-group">
- @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Efternavn", required = "required" } })
- @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
-
- </div>
-
-
-
- <div class="form-group">
- <p style="font-family: Arial-narrow; font-size:11pt; color: white; margin-bottom:1px;">Eks: 1995-01-30 (år/måned/dag)</p>
-
- @Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control", @placeholder = "Fødselsdato", required = "required" } })
- @Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
- </div>
-
- <div class="form-group">
- <label class="checkbox-inline" style="color:white;"><input name="radio1" type="radio" value="option1" id="option1" checked="" onClick="validateDays();"> Mand</label>
- <label class="checkbox-inline" style="color:white;"><input name="radio1" type="radio" value="option2" id="option2" onClick="validateDays();"> Kvinde</label>
-
- </div>
-
-
-
- <div class="form-group">
-
-
- <!--En af værdierne fra disse dropdownlister, skal gemmes og sendes videre, så det senere kan smides i databasen-->
-
- <div id="Man">
- @Html.DropDownList("SeriesID_Man", null, "Vælg serie / årgang", new { @class = "form-control", id = "ddlMan", string.Empty })
-
- </div>
- <div id="Women" style="display:none;">
- @Html.DropDownList("SeriesID_Women", null, "Vælg serie / årgang", new { @class = "form-control", id = "ddlWomen", string.Empty })
-
- </div>
-
-
-
-
-
-
-
- @*@Html.ValidationMessageFor(model => model.SeriesID, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>*@
-
- </div>
-
-
-
- <div class="form-group">
-
- @Html.DropDownList("ClubListID", null, "Vælg en klub", new { @class = "form-control", required = "required" })
- @Html.ValidationMessageFor(model => model.ClubName, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
-
- </div>
-
- <div class="form-group">
-
-
- @Html.DropDownList("PositionID", null, "Vælg Position", new { @class = "form-control", required = "true" })
- @Html.ValidationMessageFor(model => model.PositionID, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
-
- </div>
-
- <div class="form-group">
- @Html.EditorFor(model => model.UserMail, new { htmlAttributes = new { @class = "form-control", @placeholder = "E-mail adresse", required = "true" } })
- @Html.ValidationMessageFor(model => model.UserMail, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
-
- </div>
- <div class="form-group">
-
- @Html.EditorFor(model => model.UserPassword, new { htmlAttributes = new { @class = "form-control", @placeholder = "Adgangskode", required = "true" } })
- @Html.ValidationMessageFor(model => model.UserPassword, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
- </div>
-
- <div class="form-group">
- @Html.EditorFor(model => model.UserPasswordRepeat, new { htmlAttributes = new { @class = "form-control", @placeholder = "Gentag adgangskode", required = "required" } })
- @Html.ValidationMessageFor(model => model.UserPasswordRepeat, "", new { @class = "text-danger" })
- <div id="errorMessageDiv" class="errorMessage">Vær venlig at udfylde felterne.</div>
-
- </div>
-
- <div class="form-group">
-
- <input type="submit" value="Opret spiller" class="btn btn-info btn-block" style="margin-bottom:50px" />
-
- </div>
-
-
-
- @ViewBag.Succes
-
-
-
-
- }
-
-
-
-
-
- </div>
-
- <div class="col-md-4"></div>
- </div>
- </div>
-
- <script>
- var form = document.getElementById('formID'); // form has to have ID: <form id="formID">
- form.addEventListener('submit', function (event)
- { // listen for form submitting
- if (!event.target.checkValidity())
- {
- event.preventDefault(); // dismiss the default functionality
- document.getElementById('errorMessageDiv').style.display = 'block';
- // error message
- return false;
- }
-
- else
- {
- $.post('@Url.Action("CreatePlayer", "Player")');
- }
-
- }, false);
-
- </script>
View model
- using Scoutpocket.Models.Database;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace Scoutpocket.ViewModels
- {
- [MetadataType(typeof(PlayerMetaData))]
- public class PlayerProfileViewModel
- {
- public HttpPostedFileBase MyFile { get; set; }
-
- public string CroppedImagePath { get; set; }
-
- public int PlayerID { get; set; }
-
- //Profile
- [Required(ErrorMessage = "Fornavn skal udfyldes.")]
- [DisplayName("Fornavn")]
- public string FirstName { get; set; }
-
- [Required(ErrorMessage = "Efternavn skal udfyldes.")]
- [DisplayName("Efternavn")]
- public string LastName { get; set; }
-
- //club
-
- //club
-
- //club
- [Required(ErrorMessage = "Vælg venligst en klub")]
-
- //parse to clubID column in club.dbo
- public int ClubListID { get; set; }
-
- public string ClubName { get; set; }
-
- public int ID { get; set; }
-
- //the id from clublist
- public int Club_ID { get; set; }
-
- public int PositionID { get; set; }
- public int Position_ID { get; set; }
-
- public int SeriesID { get; set; }
-
- //public int Series_ID { get; set; }
-
- public int? SeriesID_Man { get; set; }
-
- public int? SeriesID_Women { get; set; }
-
- // Birth
- public DateTime BirthDate { get; set; }
-
- //User
- [Required(ErrorMessage = "E-mail adressen på kontaktpersonen skal udfyldes.")]
- [DisplayName("e-mail")]
- [Index("UserMailIndex", IsUnique = true)]
- public string UserMail { get; set; }
-
- [Required(ErrorMessage = "Adgangskoden skal udfyldes.")]
- [DataType(DataType.Password)]
- [DisplayName("adgangskode")]
- public string UserPassword { get; set; }
-
-
-
-
- [Required(ErrorMessage = "Adgangskoden skal gentages.")]
- [DataType(DataType.Password)]
- [DisplayName("Gentag adgangskode")]
- public string UserPasswordRepeat { get; set; }
- public int RoleID { get; set; }
-
-
-
- public virtual Player player { get; set; }
- }
-
- class PlayerMetaData
- {
- [Remote("IsUserExists", "Player", ErrorMessage = "Denne e-mail adresse er allerede i brug")]
- public string UserMail { get; set; }
- }
- }
Controllor
- [Route("opret-spiller")]
- public ActionResult CreatePlayer()
- {
-
-
- ViewBag.ClubListID = new SelectList(db.ClubLists, "ClubListID", "ClubName");
- ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "PositionName", "PositionCategory", 0);
-
-
-
- ViewBag.SeriesID_Man = new SelectList(db.Seriess.Where(x => x.GenderID == 0), "SeriesID", "SeriesName", "SeriesCategory", 0);
- ViewBag.SeriesID_Women = new SelectList(db.Seriess.Where(x => x.GenderID == 1), "SeriesID", "SeriesName", "SeriesCategory", 0);
-
-
- return View();
- }
-
- [Route("opret-spiller")]
- [ValidateInput(false)]
- [HttpPost]
- public ActionResult CreatePlayer(PlayerProfileViewModel viewModel, Player player)
- {
- ViewBag.ClubListID = new SelectList(db.ClubLists, "ClubListID", "ClubName", player.ClubListID).SelectedValue;
- ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "PositionName", "PositionCategory", player.PositionID).SelectedValue;
-
-
- ViewBag.SeriesID_Man = new SelectList(db.Seriess.Where(x => x.GenderID == 0), "SeriesID", "SeriesName", "SeriesCategory", 0).SelectedValue;
- ViewBag.SeriesID_Women = new SelectList(db.Seriess.Where(x => x.GenderID == 1), "SeriesID", "SeriesName", "SeriesCategory", 0).SelectedValue;
-
-
- Session["viewModel"] = viewModel;
- Session["player"] = player;
-
-
- Session["SeriesMAN"] = ViewBag.SeriesID_Man;
-
- return RedirectToAction("ChoosePlayerAbonnement");
-
- }
Database / Model
- public void CreatePlayer(PlayerProfileViewModel viewModel, Player player, int subscriptionID, int price, int transactionID, string orderID, string product)
- {
-
- var user = new User()
- {
-
- UserMail = viewModel.UserMail,
- UserPassword = viewModel.UserPassword,
- RoleID = 3
-
- };
-
- var profile = new Profile()
- {
- FirstName = viewModel.FirstName,
- Image = "User.jpg",
- LastName = viewModel.LastName
-
- };
-
- var addPlayer = new Player()
- {
-
- ClubListID = player.ClubListID,
- PositionID = player.PositionID,
-
-
-
- SeriesID = player.SeriesID,
- SeriesID_Man= player.SeriesID,
- SeriesID_Women = player.SeriesID,
- BirthDate = player.BirthDate
-
-
-
- };
-
-
-
-
- // ((List<Player>)player.clublist.Players).Add(player);
-
- //make the subscription
- var subscription = new Subscriptions()
- {
- SubscriptionIDEpay = subscriptionID,
- Price = price / 100,
- OrderID = orderID,
- TransactionID = transactionID,
- Active = true
-
-
- };
-
- /*
- var order = new Order()
- {
- OrderIDEpay = orderID,
- Price = price / 100,
- Product = product,
- Brought = DateTime.Now,
- UserID = user.UserID
- };
- */
-
-
- //Makes a relation
- user.profile = profile;
- //user.Order = order;
- addPlayer.profile = profile;
- subscription.profile = profile;
- //order.User = user;
-
-
-
-
-
-
- //db.Orders.Add(order);
- db.Users.Add(user);
- db.Profiles.Add(profile);
- db.Players.Add(addPlayer);
- db.Subscriptions.Add(subscription);
-
-
-
-
-
-
-
- db.SaveChanges();
-
-
- }