I dont know if its the way am wording my question but all i want is a way in which when a user selects a option within a drop down menu and then presses submit the value in the drop down that was selected not to be shown there. I have tried the following:
<div class="editor-field">
@Html.DropDownList("GameIDFK", String.Empty)
@Html.ValidationMessageFor(model => ViewBag.GameIDFK)
</div>
[HttpPost]
public ActionResult Create(tblReview tblreview, FormCollection formCollection)
{
if (ModelState.IsValid)
{
db.tblReviews.Add(tblreview);
db.SaveChanges();
// return RedirectToAction("Index");
}
int gameid=Convert.ToInt32(formCollection["GameIDFK"]);
var userGames = (from g in db.tblGames where g.UserName== User.Identity.Name && g.GameID!=gameid select g).ToList();
ViewBag.GameIDFK = new SelectList(userGames, "GameID", "GameName");
return View(new tblReview { UserName = @User.Identity.Name });
}
this just removes one value and when another is selected the pervious one comes back, also when i refresh the page all values return.
I have also tried the following:
<script type="text/javascript">
$(function() {
$('#GameIDFK').change(function() {
$('option:selected', this).remove();
});
});
</script>
what this does is when i click the value it totaly vanishs and when i refresh the value comes back.
I am using MVC3 C# and want a way in which when a user selects a game for example GTAIV and presses submit the value from the drop down GTAIV to be gone even when i refreash, or log out and log in again i want the value from the drop to be gone from the drop down but i dont want to delete it from the database, just want the selected value in the drop down to go once the submit button is pressed.
My controller:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Game.Models;
namespace Game.Controllers
{
public class ReviewController : Controller
{
private gamezoneDBEntities db = new gamezoneDBEntities();
//
// GET: /Review/
public ViewResult Index()
{
var Info = db.tblReviews.Include(x => x.tblGame).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
//
// GET: /Review/Details/5
public ViewResult Details(int id)
{
tblReview tblreview = db.tblReviews.Find(id);
return View(tblreview);
}
//
// 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 });
}
//
// GET: /Review/Edit/5
public ActionResult Edit(int id)
{
tblReview tblreview = db.tblReviews.Find(id);
ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName", tblreview.GameIDFK);
return View(tblreview);
}
//
// POST: /Review/Edit/5
[HttpPost]
public ActionResult Edit(tblReview tblreview)
{
if (ModelState.IsValid)
{
db.Entry(tblreview).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName", tblreview.GameIDFK);
return View(tblreview);
}
//
// GET: /Review/Delete/5
public ActionResult Delete(int id)
{
tblReview tblreview = db.tblReviews.Find(id);
return View(tblreview);
}
//
// POST: /Review/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
tblReview tblreview = db.tblReviews.Find(id);
db.tblReviews.Remove(tblreview);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
My model:
namespace Game.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 int GameIDFK { get; set; }
public string UserName { get; set; }
public virtual tblGame tblGame { get; set; }
}
}
My View:
@model Game.tblReview
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>tblReview</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ReviewID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewID)
@Html.ValidationMessageFor(model => model.ReviewID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Recomendation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Recomendation)
@Html.ValidationMessageFor(model => model.Recomendation)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AvoidOrBuy)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AvoidOrBuy)
@Html.ValidationMessageFor(model => model.AvoidOrBuy)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Score)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Score)
@Html.ValidationMessageFor(model => model.Score)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GameIDFK, "tblGame")
</div>
<div class="editor-field">
@Html.DropDownList("GameIDFK", String.Empty)
@Html.ValidationMessageFor(model => model.GameIDFK)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
I have been at this for weeks an have no outcome please help me thank you