gpt4 book ai didi

asp.net-mvc-4 - 我想在mvc4中文本框为null时显示错误消息

转载 作者:行者123 更新时间:2023-12-03 09:10:13 24 4
gpt4 key购买 nike

我是ASP.Net MVC 4的新手。我有一个文本框和一个按钮。我想在单击按钮后文本框为null时显示错误消息。

谁能帮我。
我的帐户类别是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;
namespace WebApplication6.Models
{
public class Account
{
[Required(ErrorMessage = "Please enter your name {0}")]
[StringLength(5)]
public string Name { get; set; }



[Required(ErrorMessage = "Please enter your Family {0}")]
public string Family { get; set; }
}
}

我的布局页面是:
<!DOCTYPE html>
<link href="~/Content/CSS/site.css" rel="stylesheet" />
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<header>
<div class="header">
<img src="~/Content/Images/Logo.jpg" style="position:center; border-top-color:red;" />
</div>
</header>
<div>
<h1 class="header">Wellcome to company's project</h1>

</div>
<div>
@RenderBody()
</div>
<footer>
@Html.Partial("_Footer")
</footer>
</body

>

我的 Controller 是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
[HttpGet]
public ActionResult Index()
{
return View();
}


[HttpPost]
public ActionResult LoginResult()
{
Account Oaccount = new Account();
Oaccount.Name = Request.Form["name"];
Oaccount.Family = Request.Form["family"];

/*if (Oaccount.Name=="" || Oaccount.Family=="")
{
return Content("<script language='javascript' type='text/javascript'>alert('You must enter both fields');</script>");
return RedirectToAction("Index");

}
else {*/
string Fullname=string.Format("{0} {1}", Oaccount.Name,Oaccount.Family);

ViewBag.Message = Fullname;

return View("Result");
}

我的索引 View 是:
@model WebApplication6.Models.Account 

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/Layouts/_LayoutPage.cshtml";
}
@using (Html.BeginForm(actionName: "LoginResult", controllerName: "Login"))
{
<fieldset >
<legend>Login</legend>
<p class="textbox">
Name: @Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model=>model.Name)
</p>
<p >
Family: @Html.TextBoxFor(model => model.Family)
@Html.ValidationMessageFor(model=>model.Family)
</p>
<p class="button">
<input type="submit" value="Login" />
</p>

</fieldset>
}

}
}

我的结果 View 是:
@using WebApplication6.Models
@model WebApplication6.Models.Account
@{
ViewBag.Title = "Result";
Layout = "~/Views/Shared/Layouts/_LayoutPage.cshtml";
}

<h2>Result</h2>

<fieldset>
<legend>Login Result</legend>
<p>

Wellcome
@ViewBag.Message


</p>
</fieldset>>

最佳答案

您可以使用不建议使用javascript的方式在mvc中进行验证,因为mvc提供了内置的客户端-服务器验证功能。只需使用这样的列名创建一个模型类,并提供必需的数据注释

public class Your_Class_Name
{
[Required(ErrorMessage = "Please enter {0}")]
public string Column_Name{ get; set; }
}

并将此脚本文件添加到您的项目中。
//下载这2个jquery文件并将其添加到您的“布局” View 中
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

稍后使用此类模型创建一个Strongly类型的View。

如果要显示错误消息,使用Validationmessage html帮助程序非常重要。在您的文本框后的 View 中,添加以下行
例如:
@Html.TextBoxFor(Model => Model.Column_Name)
@Html.ValidationMessageFor(Model => Model.Column_Name)

我没有得到什么问题,意味着在更改 Controller 操作方法并检查诸如以下状态的modelstate时:[HttpPost]
public ActionResult LoginResult(Account objAccount)
{
if (ModelState.IsValid)
{
Account Oaccount = new Account();
Oaccount.Name = objAccount.Name ;
Oaccount.Family = objAccount.Family ;

//Rest of the code
}
}

看看modelstate.isvalid返回什么

关于asp.net-mvc-4 - 我想在mvc4中文本框为null时显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32681447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com