2015-09-20 25 views
-1

我是新的ASP.Net MVC 4.我有一個文本框和一個按鈕。點擊按鈕後,我想在文本框爲空時顯示錯誤消息。我想顯示錯誤消息,當文本框爲空mvc4

任何人都可以幫助我。 我的賬戶類是:

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 

> 我的控制器:

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"); 
     } 

我的索引視圖是:

@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> 
} 

     } 
    } 

我的結果的看法是:

@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>> 
+0

你如何渲染該文本框?您是否使用HtmlHelper擴展? – cezarypiatek

+0

我使用html.beginform和html.textboxfor –

+0

是否有任何理由你不使用客戶端驗證(所以你開箱即可得到客戶端和服務器端驗證)?其他方面,你將需要JavaScript來處理按鈕'.click()'事件,檢查值並顯示一條消息。 –

回答

0

您可以通過使用未推薦爲MVC提供了內置的客戶端 - 服務器驗證的JavaScript做驗證的MVC。只要創建與列名模型類是這樣,並提供所需的數據詮釋

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

這個腳本文件添加到您的項目。 //下載此2 jQuery的文件,並在佈局視圖中添加它

<script src="~/Scripts/jquery.validate.min.js"></script> 
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 

後來創建一個強烈該類模型類型視圖。

如果你想顯示錯誤信息,那麼使用Validationmessage html helper非常重要。在你的文本後,您的視圖中添加此行 例:

@Html.TextBoxFor(Model => Model.Column_Name) 
@Html.ValidationMessageFor(Model => Model.Column_Name) 

我沒有得到什麼問題,平均時更改您的控制器操作方法,並檢查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回報

+0

我在我的帳戶類中添加此代碼:[必需(ErrorMessage =「請輸入{0}」)] public string Family {get;組; } –

+0

我在佈局頁面中添加這二種Javascript語言 並將這兩個文件下載並添加到腳本文件中。而且@ Html.ValidationMessageFor(Model => Model.Family)被添加到我的視圖中。但它不起作用。 –

+0

您可以發佈您的模型類,控制器的操作方法和觀點,這樣我可以去通過 – Amar

0

如果只想領域是不空,你可以使用:

@Html.TextBoxFor(model => model.Family, new { @required = "required" }) 

如果您想要的不僅僅是非空,而且需要的條件中添加條件。或者使用Rajsh的解決方案。