Sample code: Perform Addtion operation consume Wcf service in the asp.net mvc
Controller:
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(FormCollection obj)
{
Service1Client addobj = new Service1Client();
int a = Convert.ToInt32(obj[0]);
int b = Convert.ToInt32(obj[1]);
ViewBag.Result = a + b;
ViewBag.Firstvalue = a.ToString();
ViewBag.Secondvalue = b.ToString();
return View();
}
View:
@{
ViewBag.Title = "Add";
}
<style type="text/css">
.watermark
{
color:Gray;
background-color:White;
}
</style>
<h2>Add</h2>
<br />
<br />
@using (Html.BeginForm("Add", "Home"))
{
if (@ViewBag.Firstvalue != "" && @ViewBag.Firstvalue !=null)
{
var s = @ViewBag.Firstvalue;
<label> FirstValue : </label> @Html.TextBox("FirstValue", (string)@ViewBag.Firstvalue, new { @class = "watermark" })
}
else
{
<label> FirstValue : </label> @Html.TextBox("FirstValue", "FirstValue", new { @class = "watermark" })
}
<br />
if (@ViewBag.Secondvalue != "" && @ViewBag.Secondvalue != null)
{
var s = @ViewBag.Secondvalue;
<label> SecondValue : </label> @Html.TextBox("SecondValue", (string)@ViewBag.Secondvalue, new { @class = "watermark" })
}
else
{
<label> SecondValue : </label> @Html.TextBox("SecondValue", "SecondValue", new { @class = "watermark" })
}
<br />
<label> Result: @ViewBag.Result</label>
<br />
<input type="submit" value="submit" />
}
ScreenShot:


Description:
Html.BeginForm("Add", "Home")
Firstparameter:action name -->Add
Second Paramter:Controllername -->Home
Controller:
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(FormCollection obj)
{
Service1Client addobj = new Service1Client();
int a = Convert.ToInt32(obj[0]);
int b = Convert.ToInt32(obj[1]);
ViewBag.Result = a + b;
ViewBag.Firstvalue = a.ToString();
ViewBag.Secondvalue = b.ToString();
return View();
}
View:
@{
ViewBag.Title = "Add";
}
<style type="text/css">
.watermark
{
color:Gray;
background-color:White;
}
</style>
<h2>Add</h2>
<br />
<br />
@using (Html.BeginForm("Add", "Home"))
{
if (@ViewBag.Firstvalue != "" && @ViewBag.Firstvalue !=null)
{
var s = @ViewBag.Firstvalue;
<label> FirstValue : </label> @Html.TextBox("FirstValue", (string)@ViewBag.Firstvalue, new { @class = "watermark" })
}
else
{
<label> FirstValue : </label> @Html.TextBox("FirstValue", "FirstValue", new { @class = "watermark" })
}
<br />
if (@ViewBag.Secondvalue != "" && @ViewBag.Secondvalue != null)
{
var s = @ViewBag.Secondvalue;
<label> SecondValue : </label> @Html.TextBox("SecondValue", (string)@ViewBag.Secondvalue, new { @class = "watermark" })
}
else
{
<label> SecondValue : </label> @Html.TextBox("SecondValue", "SecondValue", new { @class = "watermark" })
}
<br />
<label> Result: @ViewBag.Result</label>
<br />
<input type="submit" value="submit" />
}
ScreenShot:
Description:
Html.BeginForm("Add", "Home")
Firstparameter:action name -->Add
Second Paramter:Controllername -->Home