title: I want to show error message when textbox is null in mvc4
body: <p>I am new in ASP.Net MVC 4. I have one textbox and one button. I want to show the error message when textbox is null after clicking on the button.</p>
  
  <p>can anyone help me.
  My Account class is:</p>
  
  <pre><code>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; }
      }
  }
  </code></pre>
  
  <p>My Layout page is:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;link href="~/Content/CSS/site.css" rel="stylesheet" /&gt;
  &lt;script src="~/Scripts/jquery.validate.min.js"&gt;&lt;/script&gt;
  &lt;script src="~/Scripts/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt; 
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;@ViewBag.Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;header&gt;
          &lt;div class="header"&gt;
              &lt;img src="~/Content/Images/Logo.jpg" style="position:center; border-top-color:red;" /&gt;
          &lt;/div&gt;
      &lt;/header&gt;
      &lt;div&gt;
          &lt;h1 class="header"&gt;Wellcome to company's project&lt;/h1&gt;
  
      &lt;/div&gt;
      &lt;div&gt;
          @RenderBody()
      &lt;/div&gt;
     &lt;footer&gt;
         @Html.Partial("_Footer")
     &lt;/footer&gt;
  &lt;/body
  </code></pre>
  
  <p>>
  
  My controller is:</p>
  
  <pre><code>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("&lt;script language='javascript' type='text/javascript'&gt;alert('You must enter both fields');&lt;/script&gt;");
                  return RedirectToAction("Index");
  
              } 
              else {*/
             string Fullname=string.Format("{0} {1}", Oaccount.Name,Oaccount.Family);
  
                  ViewBag.Message = Fullname;
  
              return View("Result");
          }
  </code></pre>
  
  <p>My Index View is:</p>
  
  <pre><code>@model WebApplication6.Models.Account 
  
  @{
      ViewBag.Title = "Index";
      Layout = "~/Views/Shared/Layouts/_LayoutPage.cshtml";
  }
  @using (Html.BeginForm(actionName: "LoginResult", controllerName: "Login"))
  {
      &lt;fieldset &gt;
          &lt;legend&gt;Login&lt;/legend&gt;
          &lt;p class="textbox"&gt;
              Name: @Html.TextBoxFor(model =&gt; model.Name)
              @Html.ValidationMessageFor(model=&gt;model.Name)
          &lt;/p&gt;
          &lt;p &gt;
              Family: @Html.TextBoxFor(model =&gt; model.Family)
              @Html.ValidationMessageFor(model=&gt;model.Family)
          &lt;/p&gt;
          &lt;p class="button"&gt;
              &lt;input type="submit" value="Login" /&gt; 
          &lt;/p&gt;
  
      &lt;/fieldset&gt;
  }
  
          }
      }
  </code></pre>
  
  <p>And my Result view is:</p>
  
  <pre><code>@using WebApplication6.Models
  @model WebApplication6.Models.Account
  @{
      ViewBag.Title = "Result";
      Layout = "~/Views/Shared/Layouts/_LayoutPage.cshtml";
  }
  
  &lt;h2&gt;Result&lt;/h2&gt;
  
  &lt;fieldset&gt;
      &lt;legend&gt;Login Result&lt;/legend&gt;
      &lt;p&gt;
  
          Wellcome
          @ViewBag.Message
  
  
      &lt;/p&gt;
  &lt;/fieldset&gt;&gt;
  </code></pre>