I have the following code in my controller:
// GET: /Review/Create
public ActionResult Create()
{
var userGames = db.tblGames.Where(g => g.UserName == User.Identity.Name);
ViewBag.GameIDFK = new SelectList(userGames, "GameID", "GameName");
return View(new tblReview { UserName = @User.Identity.Name });
}
// POST: /Review/Create
[HttpPost]
public ActionResult Create(tblReview tblreview)
{
if (ModelState.IsValid)
{
db.tblReviews.Add(tblreview);
db.SaveChanges();
return RedirectToAction("Index");
}
var userGames = db.tblGames.Where(g => g.UserName == User.Identity.Name);
ViewBag.GameIDFK = new SelectList(userGames, "GameID", "GameName");
return View(new tblReview { UserName = @User.Identity.Name });
}
And i want to add this line of code to it:
return View(new tblReview { Posted = DateTime.Now });
But i dont know how to combine the code together. I have tried a If statement but the application fell apart as in it was just throwing errors everywhere.
The reason i want to add these to codes together is so when a review is written the viewer can see the date the review was posted. I have a posted attribute in my database table and model for that looks like this:
namespace TestWebSite.Models
{
using System;
using System.Collections.Generic;
public partial class tblReview
{
public int ReviewID { get; set; }
public string Recomendation { get; set; }
public string AvoidOrBuy { get; set; }
public string Score { get; set; }
public System.DateTime Posted { get; set; }
public int GameIDFK { get; set; }
public string UserName { get; set; }
public virtual tblGame tblGame { get; set; }
}
}
Thank you
If you require addtional information please ask me
I admint that I don’t understand extactly the relation form
tblReviewandNewsItem, but if you want to expose that information without setting it in the ViewModel, I think the only way is to use the ViewBag.Something like
and then accessing it from the view.
I honestly would create a specific viewmodel holding all the information the view needs, but if there is not other way I guess this is an alternative.
Update
After the change you made I don’t see why this should not work