我有: GuestResponseRepository,IRepository - >接口,HomeController - >控制,谢谢 - >图
GuestReresponseRepository有一个包含响应的列表。当我通过表单添加响应时,响应将添加到列表中。当您尝试使用相同的值第二次添加响应时,AddReponse方法返回false。我想在视图中使用bool来显示不同的文本。我怎样才能做到这一点?在我的代码下面
GuestResponseRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PartyInvites.Abstract;
namespace PartyInvites.Models
{
public class GuestResponseRepository : IRepository
{
private static List<GuestResponse> responses = new List<GuestResponse>();
IEnumerable<GuestResponse> IRepository.GetAllResponses()
{
return responses;
}
bool IRepository.AddResponse(GuestResponse response)
{
if (responses.Any(x => x.Email == response.Email)) //here
{
if (responses.Any(x => x.WillAttend == response.WillAttend)) //here
{
return false;
}
var attend = responses.First(x => x.Email == response.Email && x.WillAttend != response.WillAttend);
attend.WillAttend = response.WillAttend;
return true;
}
responses.Add(response);
return true;
}
}
}
IRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PartyInvites.Models;
namespace PartyInvites.Abstract
{
public interface IRepository
{
IEnumerable<GuestResponse> GetAllResponses();
bool AddResponse(GuestResponse response);
}
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyInvites.Models;
using PartyInvites.Abstract;
namespace PartyInvites.Controllers {
public class HomeController : Controller {
private IRepository repository;
public HomeController(IRepository iRepository)
{
this.repository = iRepository;
}
public ViewResult Index() {
return View(repository.GetAllResponses());
}
public ViewResult PartyPeople()
{
return View(repository.GetAllResponses());
}
[HttpGet]
public ViewResult RsvpForm() {
return View();
}
[HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse) {
if (ModelState.IsValid) {
repository.AddResponse(guestResponse);
return View("Thanks", guestResponse);
}
else
{
// there is a validation error
return View();
}
}
}
}
视图
@model PartyInvites.Models.GuestResponse
@{
ViewBag.Title = "Thanks";
}
<div class="text-center">
<h1>Thank you, @Model.Name!</h1>
<div class="lead">
@if (Model.WillAttend == true) {
@:It's great that you're coming. The drinks are already in the fridge!
} else {
@:Sorry to hear that you can't make it, but thanks for letting us know.
}
@if (bool from Addresponse is false) {
@:<p>You already submitted this response. Are you sure this is right?</p>
}
</div>
<a href="/Home/PartyPeople">Link naar pp</a>
</div>
答案 0 :(得分:2)
从布尔变量中的AddResponses捕获返回值,并在ViewBag object
中设置自定义属性 bool result = repository.AddResponse(guestResponse);
ViewBag.Response = result;
return View("Thanks", guestResponse);
现在在Thanks
视图中,您可以使用此属性来决定要写入的文本
@model PartyInvites.Models.GuestResponse
@{
ViewBag.Title = "Thanks";
bool response = (ViewBag.Response != null ? Convert.ToBoolean(ViewBag.Response) : false);
}
.....
@if (!response) {
@:<p>You already submitted this response. Are you sure this is right?</p>
}