我已经向模型添加了验证,但是当我尝试在控制器中强制执行验证时,似乎无法检测到代码无效并转到错误的视图。
我的想法是,由于某种原因,它没有查看obj并绕过了它,但是我找不到找到方法来对其进行检查。
有人可以帮助我吗?
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TLA_Message_Board.Models;
namespace TLA_Message_Board.Controllers
{
public class MessagesController : Controller
{
// GET: Messages
public ActionResult Load()
{
Messages obj = new Messages {
Sender = "Ross McKenzie",
Receiver = "Noah McKenzie",
Year = 9,
StartDate = new DateTime(2018, 06, 28),
Expiry = new DateTime(2018, 06, 29),
Title = "Title Test",
Message = "This is the first message for the test of the internal message board."
};
return View("Messages",obj);
}
public ActionResult Enter()
{
return View("EnterMessages");
}
public ActionResult Submit()
{
Messages obj = new Messages();
obj.Sender = Request.Form["Sender"];
obj.Receiver = Request.Form["Receiver"];
string year = Request.Form["Year"];
obj.Year = Int32.Parse(year);
string start = Request.Form["StartDate"];
DateTime startParse = DateTime.Parse(start);
obj.StartDate = startParse;
string expiry = Request.Form["Expiry"];
DateTime expiryParse = DateTime.Parse(expiry);
obj.StartDate = expiryParse;
obj.Title = Request.Form["Title"];
obj.Message = Request.Form["Message"];
if(ModelState.IsValid)
{
return View("Messages", obj);
}
else
{
return View("EnterMessages");
}
}
}
}
控制器:
{{1}}