想做个类似购物后评论的功能,显示一次后关闭该链接或替换成其他文字什么的。
给你完整做了一个
<%@ 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; }
}
}