Asp.net mvc5 value dosen't save to the database

一曲冷凌霜 提交于 2020-01-06 15:21:09

问题


Right now I have a MVC5 project that looks like this when creating a post:

So I do get a Drop down lsit for both GameId and NextGame which is how I want it. How ever when creating this post only 'GameId' gets saved to the database. While the NextGame Tab would be left at null. Any idea on how I would go a head and fix this?

This is my 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 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;
        }
    }

And here is my PostModelCreate method

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

And this is my view where I create the dropdown lists

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


Not sure how your game Table is set up but this may help.

In your Model Change NextGame to 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;
        }
    }
}

In your Controller change The following

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

Controller;

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

View:

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


来源:https://stackoverflow.com/questions/31115142/asp-net-mvc5-value-dosent-save-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!