【发布时间】:2014-03-13 12:02:14
【问题描述】:
我面临一个问题我想使用 Razor 在 MVC4 中的 Popup 中显示错误消息。我在我的模型和表单提交中使用不同的验证消息,如果失败我想显示相同的验证消息在我的模型中给出。我正在与您分享我的模型、视图和控制器代码。有人可以帮我这样做吗
型号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
namespace Employee_Mgmt_System.Models
{
public class LoginScreen
{
[Required(ErrorMessage = "EmployeeID Cannot be Blank")]
public string EmpID
{
get;
set;
}
[Required(ErrorMessage = "Password Cannot be kept Blank")]
[DataType(DataType.Password)]
public string Password
{
get;
set;
}
}
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Employee_Mgmt_System.Models;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Employee_Mgmt_System.Controllers
{
public class LoginScreenController : Controller
{
//
// GET: /LoginScreen/
LoginScreen Ls = new LoginScreen();
[HttpPost]
public ActionResult Login(LoginScreen LogScreen)
{
if (ModelState.IsValid)
{
return RedirectToAction("HomeScreen", "HomeScreen");
}
return View("LoginScreen");
}
public ActionResult LoginScreen()
{
return View("LoginScreen");
}
}
}
查看
@model Project.LoginScreen
@{
ViewBag.Title = "LoginScreen";
}
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<h2>LoginScreen</h2>
<body>
@using(Html.BeginForm("login","loginscreen",FormMethod.Post))
{
@Html.ValidationSummary(true)
<div style="color:red;text-align:center">
<fieldset>
<legend>Validation Summary</legend>
@Html.ValidationMessageFor(m=>m.EmpID)
<br />
@Html.ValidationMessageFor(m=>m.Password)
</fieldset>
</div>
<br />
<br />
<div>
<table border="1" style="background-color:activeborder">
<tr>
<td>
@Html.LabelFor(@m=>@m.EmpID)
</td>
<td>
@Html.TextBoxFor(@m=>@m.EmpID)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(@m=>@m.Password)
</td>
<td>
@Html.PasswordFor(@m=>@m.Password)
</td>
</tr>
</table>
<input type="submit" value="login" />
</div>
}
</body>
【问题讨论】:
标签: jquery asp.net-mvc asp.net-mvc-4 razor