Asp.net '1' is not valid.

I am trying to work on an asp.net mvc5 project where I have two Post and Game models.

It will work like this, you publish a message, and you choose which game you made and also choose which game you are going to do next. As you can see in this image, I got it so that you got a DropDownList from the games that are in the database: enter image description here

As always, when I click Create, I get an error. The value "1" is invalid. enter image description here

And I'm not sure why this will happen, as in my database. I save these values ​​as int. enter image description herePost Model:

public class Post
    {
        [Key]
        public int PostId { get; set; }

        //URL
        [Display(Name = "URL")]
        [StringLength(80)]
        [DataType(DataType.Url)]
        [Required]
        public string Url { get; set; }
        //User
        [Display(Name = "User")]
        public virtual ApplicationUser User { get; set; }

        //Game
        [Display(Name = "Game")]
        public virtual Game GameId { get; set; }

        [Display(Name = "Next Game")]
        public virtual Game NextGameId { get; set; }

        //Time
        private DateTime? _date;
        public DateTime? Date
        {
            get
            {
                if (_date == null || _date.ToString() == "1/1/0001 12:00:00 AM")
                {
                    return _date = DateTime.Now;
                }
                return _date;
            }
            set
            {
                _date = value;
            }
        }
    }

Mail Controller:

 // GET: Posts/Create
    public ActionResult Create()
    {
        ViewBag.GameId = new SelectList(db.Games, "GameId", "GameTitle");
        ViewBag.NextGameId = new SelectList(db.Games, "GameId", "GameTitle");

        return View();
    }

    // POST: Posts/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "PostId,Url,GameId,NextGameId,Date")] Post post)
    {
        if (ModelState.IsValid)
        {
            db.Posts.Add(post);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        ViewBag.GameId = new SelectList(db.Games, "GameId", "GameTitle", post.GameId);
        ViewBag.NextGameId = new SelectList(db.Games, "GameId", "GameTitle", post.NextGameId);

        return View(post);
    }

Game Model:

public class Game
{
 [Key]
    public int GameId { get; set; }
    public string GameTitle { get; set; }
}

And my Messages / Create view , where I create drop-down menus:

    <div class="form-group">
        @Html.LabelFor(model => model.GameId, "GameId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GameId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.GameId, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.NextGameId, "NextGame", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("NextGameId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.NextGameId, "", new { @class = "text-danger" })
        </div>
    </div>
+4
1

DropDownList() typeof Game, (a <select> ). int. 2 Game,

[ForeignKey("Game")]
public int GameId { get; set; }
public virtual Game Game { get; set; }

[ForeignKey("NextGame")]
public int NextGameId { get; set; }        
public virtual Game NextGame { get; set; }

, , dropdownlists

ViewBag.GameList= new SelectList(db.Games, "GameId", "GameTitle");

@Html.DropDownListFor(m => m.GameId, (SelectList)ViewBag.GameList , new { @class = "form-control" })
@Html.DropDownListFor(m => m.NextGameId, (SelectList)ViewBag.GameList , new { @class = "form-control" })

: Date

public DateTime Date { get; set; }

public class Post
{
  public Post()
  {
    Date = DateTime.Today;
  }
+2

All Articles