Asp.net mvc5值不会保存到数据库中

时间:2015-06-29 12:00:04

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

现在我有一个MVC5项目在创建帖子时看起来像这样: enter image description here

所以我确实得到了GameId和NextGame的下拉lsit,这就是我想要的。 无论如何只创建这篇文章' GameId'被保存到数据库中。 而NextGame选项卡将保留为null。 关于我如何解决这个问题的任何想法?

这是我的帖子模型

   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 int GameId { get; set; }

    public string NextGame { get; set; }

    public virtual Game Game { 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;
        }
    }

这是我的PostModelCreate方法

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

        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,NextGame,Date")] Post post)
    {
        if (ModelState.IsValid)
        {
            db.Posts.Add(post);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        ViewBag.GameId = new SelectList(db.Games, "GameId", "Title", post.GameId);
        ViewBag.NextGame = new SelectList(db.Games, "NextGame", "Title", post.NextGame);

        return View(post);
    }

这是我创建下拉列表的视图

    <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.NextGame, "NextGame", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("NextGame", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.NextGame, "", new { @class = "text-danger" })
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

不确定你的游戏桌是如何设置的,但这可能会有所帮助。

在您的模型中将NextGame改为NextGame Id

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

    public int GameRating { get; set; }


}
public class PostInfo
{
    [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 ICollection<Game> GameId { get; set; }

    public virtual ICollection<Game> NextGameID { get; set; }
//Or use this
    public virtual Game Game { get; set; }
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;
        }
    }
}

在您的控制器中更改以下

ViewBag.NextGame = new SelectList(db.Games, "GameId", "Title", post.NextGame);


public async Task<ActionResult> Create([Bind(Include = "PostId,Url,GameId,NextGameId,Date")] Post post)

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

控制器;

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

        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", "Title", post.GameId);
        ViewBag.NextGameId = new SelectList(db.Games, "GameId", "Title", post.NextGameId);

        return View(post);
    }

查看:

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