0

Hi guys i want a way where when a user selects a value from a drop down and inputs the values that are on the interface and then value from the drop down is cleared and no longer able to be selected.

Here is 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; }
    }
}

and here is 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);
        }
    }
}

and here is the 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>

Your Help will be much thanked

if there is anything that is required please ask.

user1137472
  • 345
  • 2
  • 5
  • 20
  • 2
    I'm a little confused about what you're asking. So the user sees a form. They click the dropdown, and from the dropdown they select "Warcraft II". Now you want to immediately remove Warcraft II from the list? – StriplingWarrior Apr 07 '12 at 03:21
  • yes from the dropdown list not the database – user1137472 Apr 07 '12 at 04:36
  • That is correct what you said – user1137472 Apr 07 '12 at 04:46
  • when they submit and it redirected back to the same view or when they just select onchange – COLD TOLD Apr 07 '12 at 05:26
  • I want it so when they submit the value it is redirected to the same page so they can select another game an so on but the value that has allready been selected should not show in the list – user1137472 Apr 07 '12 at 15:45
  • @user1137472: Can you take some time to flesh out your requirements a little more? Your question and your response to my comment made it sound like you wanted the selected value to disappear the moment it was selected. Now I understand that you want the value to stay there until the user clicks "Submit," and then have the same page appear without that value in the dropdown menu. Are you trying to track this on a per-user basis, so each user can only enter one review per game? – StriplingWarrior Apr 08 '12 at 02:23

1 Answers1

0

it may not be the best way to do it but I would give it a try I did not understand why you are redirecting to // return RedirectToAction("Index"); and then return a view??/ you might also get a id from tblreview without the need of form collection

 <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 });
    }
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • It removes one value and then when i select the another value the pervious value comes back. I wanted it so when a user selects a game i.e gta and then presses submit the value goes away and not to be selected again like my post states. – user1137472 Apr 07 '12 at 15:42
  • also when i log off the value comes back. I want a way which a user selects a game inputs all the values on the interface and then submits tha value goes from the drop down an no longer be able to selected. So once a review is entered that is it the game can no longer be selected thanks for the answer was pretty close to what i wanted thanks – user1137472 Apr 07 '12 at 15:50
  • now i understand you more clear it more toward database design in this case I can recommend creating a temp table so this table would contain all the user id and games he selected and when you create another drop-down you would check if the games are in temp table for the user you would remove it from drop-down – COLD TOLD Apr 07 '12 at 17:56