asp.net值' 1'无效

时间:2015-06-30 10:53:44

标签: c# asp.net asp.net-mvc-5

我正在尝试使用asp.net mvc5项目,我有两个模型Post和Game。

它会像这样工作,你在Post上发帖,你选择你制作的游戏,你也选择下一个你要做的游戏。正如您在此图像上看到的那样,我得到了一个DropDownList,其中包含数据库中的游戏: enter image description here

当我按下Create时我得到一个错误说出价值' 1'是无效的。 enter image description here

而且我不确定为什么会发生这种情况,因为在我的数据库中我将​​这些值保存为int' s。enter image description here 帖子模型:

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

帖子控制器:

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

游戏模型:

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

我的帖子/创建视图我在哪里创建了下拉列表:

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

1 个答案:

答案 0 :(得分:2)

视图中的DropDownList()方法正在尝试绑定到作为复杂对象的Game类型(<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; }

接下来,在控制器中,如果两个下拉列表都包含相同的值,则只需生成一个选择列表

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