Asp.net mvc5值这么想的保存到数据库 [英] Asp.net mvc5 value dosen't save to the database

查看:139
本文介绍了Asp.net mvc5值这么想的保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个MVC5项目创建一个帖子的时候,看起来像这样:

所以,我得到一个下拉lsit为游戏ID和NextGame这是我多么希望它。
创建这个帖子的时候怎么过的唯一的游戏ID被保存到数据库中。
虽然NextGame选项卡将在空留。
我如何会去一个头,解决这个问题的任何想法?

这是我的Post模型

 公共类帖子
{
    [键]
    公众诠释{帖子ID获取;组; }    // URL
    [显示(名称=URL)]
    [StringLength(80)]
    [数据类型(DataType.Url)
    [需要]
    公共字符串网址{搞定;组; }
    //用户
    [显示(名称=用户)]
    公共虚拟ApplicationUser用户{搞定;组; }    //游戏
    [显示(名称=游戏)]
    公众诠释游戏ID {搞定;组; }    公共字符串NextGame {搞定;组; }    公共虚拟游戏游戏{搞定;组; }    //时间
    私人的DateTime? _日期;
    公众的DateTime?日期
    {
        得到
        {
            如果(_date == NULL || _date.ToString()==1/1/0001 12:00:00 AM)
            {
                返回_date = DateTime.Now;
            }
            归期;
        }
        组
        {
            _date =价值;
        }
    }

这是我的PostModelCreate方法

  // GET:帖子/创建
    公众的ActionResult的Create()
    {
        ViewBag.GameId =新的SelectList(db.Games,游戏ID,标题);
        ViewBag.NextGame =新的SelectList(db.Games,NextGame,标题);        返回查看();
    }    // POST:帖子/创建
    //从overposting攻击保护,请启用要绑定到特定的属性,
    //更多详情请参阅http://go.microsoft.com/fwlink/?LinkId=317598。
    [HttpPost]
    [ValidateAntiForgeryToken]
    公共异步任务<&的ActionResult GT;创建([绑定(包括=帖子ID,网址,游戏ID,NextGame,日期」)后后)
    {
        如果(ModelState.IsValid)
        {
            db.Posts.Add(岗位);
            等待db.SaveChangesAsync();
            返回RedirectToAction(「指数」);
        }        ViewBag.GameId =新的SelectList(db.Games,游戏ID,标题,post.GameId);
        ViewBag.NextGame =新的SelectList(db.Games,NextGame,标题,post.NextGame);        返回查看(后);
    }

这是我的看法,我创建的下拉列表

 < D​​IV CLASS =表单组>
        @ Html.LabelFor(型号=> model.GameId,游戏ID,htmlAttributes:新{@class =控制标签COL-MD-2})
        < D​​IV CLASS =COL-MD-10>
            @ Html.DropDownList(游戏ID,空,htmlAttributes:新{@class =表格控})
            @ Html.ValidationMessageFor(型号=> model.GameId,新{@class =TEXT-危险})
        < / DIV>
    < / DIV>
    < D​​IV CLASS =表单组>
        @ Html.LabelFor(型号=> model.NextGame,NextGame,htmlAttributes:新{@class =控制标签COL-MD-2})
        < D​​IV CLASS =COL-MD-10>
            @ Html.DropDownList(NextGame,空,htmlAttributes:新{@class =表格控})
            @ Html.ValidationMessageFor(型号=> model.NextGame,新{@class =TEXT-危险})
        < / DIV>
    < / DIV>


解决方案

不知道你的游戏表是如何设置的,但是这可能会有所帮助。

在你的模型更改NextGame到NextGame标识

 公共类游戏
{
    [键]
    公众诠释游戏ID {搞定;组; }
    公共字符串GameTitle {搞定;组; }
    公共字符串GameDisc {搞定;组; }    公众诠释GameRating {搞定;组; }
}
公共类PostInfo
{
    [键]
    公众诠释{帖子ID获取;组; }    // URL
    [显示(名称=URL)]
    [StringLength(80)]
    [数据类型(DataType.Url)
    [需要]
    公共字符串网址{搞定;组; }
    //用户
    [显示(名称=用户)]
    公共虚拟ApplicationUser用户{搞定;组; }    //游戏
    [显示(名称=游戏)]
    公共虚拟的ICollection<游戏及GT;游戏ID {搞定;组; }    公共虚拟的ICollection<游戏及GT; NextGameID {搞定;组; }
//或使用该
    公共虚拟游戏游戏{搞定;组; }
公共虚拟游戏NextGameID {搞定;组; }    //时间
    私人的DateTime? _日期;
    公众的DateTime?日期
    {
        得到
        {
            如果(_date == NULL || _date.ToString()==1/1/0001 12:00:00 AM)
            {
                返回_date = DateTime.Now;
            }
            归期;
        }
        组
        {
            _date =价值;
        }
    }
}

在您的控制器更改以下

  ViewBag.NextGame =新的SelectList(db.Games,游戏ID,标题,post.NextGame);
公共异步任务<&的ActionResult GT;创建([绑定(包括=帖子ID,网址,游戏ID,NextGameId,日期」)后后)ViewBag.NextGameId =新的SelectList(db.Games,游戏ID,标题,post.NextGameId);

控制器;

  // GET:帖子/创建
    公众的ActionResult的Create()
    {
        ViewBag.GameId =新的SelectList(db.Games,游戏ID,标题);
        ViewBag.NextGameId =新的SelectList(db.Games,游戏ID,标题);        返回查看();
    }    // POST:帖子/创建
    //从overposting攻击保护,请启用要绑定到特定的属性,
    //更多详情请参阅http://go.microsoft.com/fwlink/?LinkId=317598。
    [HttpPost]
    [ValidateAntiForgeryToken]
    公共异步任务<&的ActionResult GT;创建([绑定(包括=帖子ID,网址,游戏ID,NextGameId,日期」)后后)
    {
        如果(ModelState.IsValid)
        {
            db.Posts.Add(岗位);
            等待db.SaveChangesAsync();
            返回RedirectToAction(「指数」);
        }        ViewBag.GameId =新的SelectList(db.Games,游戏ID,标题,post.GameId);
        ViewBag.NextGameId =新的SelectList(db.Games,游戏ID,标题,post.NextGameId);        返回查看(后);
    }

查看:

 < D​​IV CLASS =表单组>
        @ Html.LabelFor(型号=> model.GameId,游戏ID,htmlAttributes:新{@class =控制标签COL-MD-2})
        < D​​IV CLASS =COL-MD-10>
            @ Html.DropDownList(游戏ID,空,htmlAttributes:新{@class =表格控})
            @ Html.ValidationMessageFor(型号=> model.GameId,新{@class =TEXT-危险})
        < / DIV>
    < / DIV>
    < D​​IV CLASS =表单组>
        @ Html.LabelFor(型号=> model.NextGameId,NextGame,htmlAttributes:新{@class =控制标签COL-MD-2})
        < D​​IV CLASS =COL-MD-10>
            @ Html.DropDownList(NextGameId,空,htmlAttributes:新{@class =表格控})
            @ Html.ValidationMessageFor(型号=> model.NextGameId,新{@class =TEXT-危险})
        < / DIV>
    < / DIV>

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>

解决方案

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>

这篇关于Asp.net mvc5值这么想的保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆