选择后删除DropDownList值

时间:2012-04-07 17:48:20

标签: c# asp.net asp.net-mvc-3 asp.net-mvc-3-areas

我不知道它是否是我的问题的方式,但我想要的是一种方式,当用户在下拉菜单中选择一个选项,然后按下提交选择的下拉列表中的值不是在那里显示。我尝试过以下方法:

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

这只删除一个值,当另一个值被选中时,前一个值会返回,当我刷新页面时,所有值都会返回。

我也尝试了以下内容:

<script type="text/javascript">
    $(function() {
        $('#GameIDFK').change(function() {
            $('option:selected', this).remove();
        });
    });
</script>

这样做的是当我点击它完全消失的值时,当我刷新值时会回来。

我正在使用MVC3 C#,并想要一种方式,当用户选择游戏,例如GTAIV并按下提交下拉GTAIV的值即使我重新删除,或者注销并再次登录我想要从下拉列表中删除的值,但我不想从数据库中删除它,只是希望在按下提交按钮后下拉列表中的选定值。

我的控制器:

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

我的模特:

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

我的观点:

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

我已经在这几个星期没有结果请帮帮我谢谢

1 个答案:

答案 0 :(得分:0)

我分析了你的问题,似乎你必须制作一些持久性机制来记住用户的选择。例如,用户删除GTA IV,那么您必须使用modyfi数据库和下拉元素的存储ID,用户ID以及布尔值IsVisible。在刷新时,您必须下载两个列表并显示可见的元素。

有了这种数据,我不知道哪种持久性实施会更好。您需要分析您的用户故事,因为您可以在cookie中存储此“可见性”选项。这取决于您的功能和要求。

编辑:

试试这个

[HttpPost]
public ActionResult Edit(tblReview tblreview, string selectedValue)
{
    if (ModelState.IsValid)
    {
        db.Entry(tblreview).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.GameIDFK = new SelectList(db.tblGames.Where(x=>x.GameID != tblreview.GameIDFK), "GameID", "GameName");
    return View(tblreview);
}