繁体   English   中英

asp.net值'1'无效

[英]asp.net value ' 1 ' is invalid

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

它会像这样工作,你发布在Post上,你选择你制作的游戏,你也选择下一个你要做的游戏。 正如你在这张图片上看到的那样,我得到了一个数据库中游戏的DropDownList: 在此输入图像描述

如果我按下Create我得到一个错误说值'1'无效。 在此输入图像描述

而且我不确定为什么会发生这种情况,因为在我的数据库中我将​​这些值保存为int。 在此输入图像描述 邮政模式:

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>

您在视图中的DropDownList()方法正在尝试绑定到typeof Game ,这是一个复杂的对象( <select>仅回发单个值类型)。 您需要绑定到typeof 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;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM