ASP.NET MVC5项目中如何添加判断让一个链接只出现一次 5C

想做个类似购物后评论的功能,显示一次后关闭该链接或替换成其他文字什么的。

1个回答

给你完整做了一个
图片说明

代码下载:https://download.csdn.net/download/caozhy/11158474

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Q757530.Models.ProductDetailsModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Details</h2>

    <fieldset>
        <legend>Fields</legend>

        <div class="display-label">id</div>
        <div class="display-field"><%: Model.id %></div>

        <div class="display-label">ProductName</div>
        <div class="display-field"><%: Model.ProductName %></div>

        <hr />

        Comments

        <% foreach (var item in Model.Comments){ %>
            <p> <%: item.Key %> : <%: item.Value %></p>
        <% } %>

        <% if (!Model.Comments.ContainsKey(User.Identity.Name))
           { %>

            <% using (Html.BeginForm())
               {%>

                    你还没有评论,你可以添加一个评论:

                            <%: Html.TextArea("txtcom")%>

                        <p>
                            <input type="submit" value="提交" />
                        </p>
                    </fieldset>

                <% } %>





        <% }
           else
           { %>

           感谢你的评论
           <% }%>



    </fieldset>
    <p>
        <%: Html.ActionLink("Edit", "Edit", new { id=Model.id }) %> |
        <%: Html.ActionLink("Back to List", "Index") %>
    </p>

</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Q757530.Models;

namespace Q757530.Controllers
{
    [Authorize]
    public class ProductsController : Controller
    {

        private DataClasses1DataContext db = new DataClasses1DataContext();

        //
        // GET: /Products/

        public ActionResult Index()
        {
            var model = db.Products;
            return View(model);
        }

        //
        // GET: /Products/Details/5

        public ActionResult Details(int id)
        {
            var model = new ProductDetailsModel() { id = id, ProductName = db.Products.Single(x => x.id == id).ProductName };
            model.Comments = db.Comments.Where(x => x.pid == id).ToDictionary(x => x.username, x => x.comment1);
            return View(model);
        }


        //
        // POST: /Products/Details/5

        [HttpPost]
        public ActionResult Details(int id, FormCollection col)
        {
            try
            {
                // TODO: Add insert logic here
                if (col["txtcom"].ToString() != "")
                {
                    if (!db.Comments.Any(x => x.pid == id && x.username == User.Identity.Name))
                    {
                        db.Comments.InsertOnSubmit(new Comment() { username = User.Identity.Name, pid = id, comment1 = col["txtcom"].ToString() });
                        db.SubmitChanges();
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Products/Create

        public ActionResult Create()
        {
            return View(new Product());
        } 

        //
        // POST: /Products/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                var model = new Product();
                UpdateModel(model);
                db.Products.InsertOnSubmit(model);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Products/Edit/5

        public ActionResult Edit(int id)
        {
            var model = db.Products.Single(x => x.id == id);
            return View(model);
        }

        //
        // POST: /Products/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                var model = new Product();
                UpdateModel(model);
                var p = db.Products.Single(x => x.id == id);
                p.ProductName = model.ProductName;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Products/Delete/5

        public ActionResult Delete(int id)
        {
            var model = db.Products.Single(x => x.id == id);
            return View(model);
        }

        //
        // POST: /Products/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var p = db.Products.Single(x => x.id == id);
                db.Products.DeleteOnSubmit(p);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Q757530.Models
{
    public class ProductDetailsModel
    {
        public int id { get; set; }
        public string ProductName { get; set; }
        public Dictionary<string, string> Comments { get; set; }
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问