【问题标题】:asp.net value ' 1 ' is invalidasp.net 值“1”无效
【发布时间】:2015-06-30 10:53:44
【问题描述】:

我正在尝试处理 asp.net mvc5 项目,其中我有两个模型 Post 和 Game。

它会像这样工作,您在 Post 上发帖并选择您制作的游戏,并且您还可以选择接下来要制作的游戏。正如你在这张图片上看到的那样,我得到了数据库中游戏的 DropDownList:

当我按下创建时,我得到一个错误,说值“1”是无效的。

我不确定为什么会发生这种情况,因为在我的数据库中我将​​这些值保存为 int's.Post 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;
            }
        }
    }

后控制器:

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

【问题讨论】:

  • 按你说的工作... 提示:[Display(Name = "Game")] public virtual Game GameId { get; set; } 应该是 int? 而不是 Game
  • 你为什么从之前的问题中改变你的模型 - GameId 和 NextGameId 属性现在是 typeof Game (一个复杂对象)。将它们改回int。并且不要将您的 ViewBag 属性命名为与模型属性相同
  • 调用导航属性GameId 和NextGameId 很混乱。
  • @StephenMuecke 因为在上一个问题中确实有人提到这样做会更好,所以我尝试了但失败了。
  • 将属性改回int。在控制器中使用ViewBag.GameList = new SelectList(db.Games, "GameId", "GameTitle")(只需要一个),在视图中使用@Html.DropDownListFor(m =&gt; m.GameId, (SelectList)ViewBag.GameList , new { @class = "form-control" }),NextGameId 同上

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2020-12-28
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多