pankaj dutt

pankaj dutt

  • NA
  • 15
  • 12.2k

mvc4 validation Message is not showing

Oct 20 2015 1:12 PM
view:
 
@model test.Models.home
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<h2>Index</h2>
@using (Html.BeginForm("SaveData","Home")){
@Html.ValidationSummary(true)
<div>
@Html.TextBoxFor(model => model.uname)
@Html.ValidationMessageFor(model=>model.uname)
</div>
<div>
@Html.TextBoxFor(model=>model.pass)
@Html.ValidationMessageFor(model=>model.pass)
</div>
<div>
@Html.TextBoxFor(model=>model.fname)
@Html.ValidationMessageFor(model=>model.fname)
</div><div>
@Html.TextBoxFor(model=>model.lname)
@Html.ValidationMessageFor(model=>model.lname)
</div>
<p><input type="submit" value="submit"></p>
}
<div>
@Html.ActionLink("Home", "SaveData")
</div>
</form>
Controller:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using test.Models;
using System.Data.SqlClient;
namespace test.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult SaveData(string uname,string pass,string fname,string lname)
{
if (ModelState.IsValid)
{
home h = new home();
h.pass = pass;
h.uname = uname;
h.fname = fname;
h.lname = lname;
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into db2(uid,pass,fname,lname)values('" + h.uname + "','" + h.pass + "','" + h.fname + "','" + h.lname + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return View("SaveData");
}
return View("Index");
}
}
}
 model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace test.Models
{
public class home
{
[Required(ErrorMessage="please give tha name")]
public string uname
{ get; set; }
[Required (ErrorMessage="please enter passsword")]
public string pass
{ get; set; }
[Required (ErrorMessage="please enter fname")]
public string fname
{ get; set; }
[Required(ErrorMessage = "please enter lname")]
public string lname
{ get; set; }
}
}
Please help me. 

Answers (3)