Saturday, 2 January 2016

How to handle multiple submit Buttons with Validation on the same form MVC Razor


Note: In this topic we will discuss how to handle multiple submit buttons with types of validation required, regular expression, compare on same MVC razor form.


/****** Sql Query ************************/

CREATE TABLE [dbo].[Employee](
[EmpId] [varchar](50) NOT NULL,
[Name] [varchar](50) NULL,
[Gender] [varchar](50) NULL,
[EmailId] [varchar](50) NOT NULL,
[Password] [varchar](50) NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[EmpId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[EmployeeInfo](
[EmpId] [varchar](50) NOT NULL,
[Address] [varchar](max) NULL,
[City] [varchar](50) NULL,
[Country] [varchar](50) NULL,
[MobileNo] [varchar](50) NULL,
CONSTRAINT [PK_EmployeeInfo] PRIMARY KEY CLUSTERED
(
[EmpId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

CREATE TABLE [dbo].[CityInfo](
[CityId] [int] NOT NULL,
[CityName] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
(
[CityId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

We will take  Mvc  Application  and  add  Controller  which name is CetpaController and  using this  controller  we  will add  View  which  name is Index 
Code for Index.cshtml………………………

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">

$(document).ready(function()
{
$('#ddlcity').change(function (e)
{
var v = $('#ddlcity').find('option:selected').text();
$('#ddlcityText').val(v);
e.preventDefault();
});
});
</script>

<title>Index</title>
</head>
<body>


<div>
<fieldset>

@using (Html.BeginForm())
{
<table>
<tr>
<td>EmpId</td>
<td>
@Html.TextBox("txtempid")
@Html.ValidationMessage("txtempid", new { @style = "color:red" })
</td>
</tr>
<tr><td>Name</td><td>
@Html.TextBox("txtname")
@Html.ValidationMessage("txtname", new { @style = "color:red" })
</td></tr>
<tr><td>Gender</td><td>@Html.RadioButton("Gender", "Male") Male @Html.RadioButton("Gender", "Female")FeMale
@Html.ValidationMessage("Gender", new { @style = "color:red;margin-left:40px" })</td></tr>
<tr><td>Emailid</td><td>@Html.TextBox("txtemailid") @Html.ValidationMessage("txtemailid", new { @style = "color:red" })</td></tr>
<tr><td>Password</td><td>@Html.Password("txtpassword") @Html.ValidationMessage("txtpassword", new { @style = "color:red" })</td></tr>
<tr><td>Confirm Password</td><td>@Html.Password("txtcpassword") @Html.ValidationMessage("txtcpassword", new { @style = "color:red" })</td></tr>

<tr><td  colspan="2"><input type="submit" value="First" name="btn" /></td></tr>
</table>

<table>
<tr>
<td>EmpId</td>
<td>
@Html.TextBox("txtempid1")
@Html.ValidationMessage("txtempid1", new { @style = "color:red" })
</td>
</tr>
<tr><td>Address</td><td>@Html.TextBox("txtaddress")@Html.ValidationMessage("txtaddress", new { @style = "color:red" })</td></tr>
<tr><td>City</td><td>@Html.DropDownList("ddlcity", "Select")
@Html.Hidden("ddlcityText") @Html.ValidationMessage("ddlcity", new { @style = "color:red" })</td></tr>
<tr><td>Country</td><td>@Html.DropDownList("ddlcountry", new[]

{
new SelectListItem { Text="Select", Value="Select" },
new SelectListItem { Text="India", Value="India"},
new SelectListItem { Text="England", Value="England" },
new SelectListItem { Text="USA", Value="USA"}
}) @Html.ValidationMessage("ddlcountry", new { @style = "color:red" })</td></tr>
<tr><td>Mobile No</td><td>@Html.TextBox("txtmobileno")@Html.ValidationMessage("txtmobileno", new { @style = "color:red" })</td></tr>
<tr><td colspan="2"><input type="submit" value="Second" name="btn" /></td></tr>
<tr><td colspan="2">@ViewBag.Message</td></tr>
</table>
}
</fieldset>

</div>
</body>
</html>


Code for CetpaController………………………
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Web.Security;
using System.Text.RegularExpressions;
using ValidationConcept.Models;

namespace ValidationConcept.Controllers
{


public class CetpaController : Controller
{
testEntities2 database = new testEntities2();

[HttpGet]
public ActionResult Index()
{

ViewBag.ddlcity = (from m in database.CityInfoes select new SelectListItem { Text = m.CityName, Value = m.CityId.ToString() }).ToList();
return View();

}


[HttpPost]
public ActionResult Index(FormCollection fc)
{

// *******************  this code for First Button....................*********//

if (fc["btn"] == "First")
{
if (string.IsNullOrEmpty(fc["txtempid"]))
{
ModelState.AddModelError("txtempid", "Please enter empid");
}
if (string.IsNullOrEmpty(fc["txtname"]))
{
ModelState.AddModelError("txtname", "Please enter name");
}

if (string.IsNullOrEmpty(fc["txtemailid"]))
{
ModelState.AddModelError("txtemailid", "Please enter emailid");
}
else if (!Regex.IsMatch(fc["txtemailid"].ToString(), @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase))
{
ModelState.AddModelError("txtemailid", "not validation format");
}

if (string.IsNullOrEmpty(fc["txtpassword"]))
{
ModelState.AddModelError("txtpassword", "Please enter password");
}
if (string.IsNullOrEmpty(fc["txtcpassword"]))
{
ModelState.AddModelError("txtcpassword", "Please enter Confirm password");
}
else if (!Regex.Equals(fc["txtpassword"], fc["txtcpassword"]))
{
ModelState.AddModelError("txtcpassword", "do not match password and confirm password");
}
if (string.IsNullOrEmpty(fc["Gender"]))
{
ModelState.AddModelError("Gender", "Please select gender");
}


if (ModelState.IsValid)
{
Employee emp = new Employee
{
EmpId=fc["txtempid"].ToString(),
Name = fc["txtname"].ToString(),
Gender = fc["Gender"].ToString(),
EmailId = fc["txtemailid"].ToString(),
Password = fc["txtpassword"].ToString()
};
database.Employees.Add(emp);
database.SaveChanges();
ViewBag.Message = "Saved successfully in First Table";
}

}

// *******************  this code for Second Button....................*********//

if (fc["btn"] == "Second")
{

//  string aa = fc["ddlcity"].ToString();
if (string.IsNullOrEmpty(fc["txtempid1"]))
{
ModelState.AddModelError("txtempid1", "Please enter empid");
}
if (string.IsNullOrEmpty(fc["txtaddress"]))
{
ModelState.AddModelError("txtaddress", "Please enter name");
}

if (fc["ddlcity"].ToString()== "")
{

ModelState.AddModelError("ddlcity", "Please select city");
}


if (fc["ddlcountry"].ToString() == "Select")
{
ModelState.AddModelError("ddlcountry", "Please select state");
}
if (string.IsNullOrEmpty(fc["txtmobileno"]))
{
ModelState.AddModelError("txtmobileno", "Please enter mobile no");
}
else if (!Regex.IsMatch(fc["txtmobileno"].ToString(), "^[0-9]"))
{
ModelState.AddModelError("txtmobileno", "Please enter only integer no");
}


if (ModelState.IsValid)
{
EmployeeInfo empinfo = new EmployeeInfo
{
EmpId = fc["txtempid1"].ToString(),
Address = fc["txtaddress"].ToString(),
City = fc["ddlcityText"].ToString(),
Country = fc["ddlcountry"].ToString(),
MobileNo = fc["txtmobileno"].ToString()
};
database.EmployeeInfoes.Add(empinfo);
database.SaveChanges();
ViewBag.Message = "Saved successfully in Second Table";
}


}
ViewBag.ddlcity = (from m in database.CityInfoes select new SelectListItem { Text = m.CityName, Value = m.CityId.ToString() }).ToList();
return View();

}
}
}

Result



1 comments:

  1. good afternoon sir , i have facing problem in wcf how to fixed

    HTTP could not register URL http://+:8070/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

    ReplyDelete