Hi lakshmi.teli9,
Please refer below sample.
SQL
Create table [Customers_Name]
(
[Id] INT IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(20)
)
INSERT INTO [Customers_Name] VALUES('John Hammond')
INSERT INTO [Customers_Name] VALUES('Mudassar Khan')
INSERT INTO [Customers_Name] VALUES('Suzanne Mathews')
INSERT INTO [Customers_Name] VALUES('Robert Schidner')
Create table [Customers_Country]
(
[Id] INT IDENTITY(1,1) PRIMARY KEY,
[CustomersId] INT,
[Country] VARCHAR(20)
)
INSERT INTO [Customers_Country] VALUES(1,'United States')
INSERT INTO [Customers_Country] VALUES(2,'India')
INSERT INTO [Customers_Country] VALUES(3,'France')
INSERT INTO [Customers_Country] VALUES(4,'Russia')
SELECT * FROM [Customers_Country]
SELECT * FROM [Customers_Name]
DROP TABLE [Customers_Name]
DROP TABLE [Customers_Country]
Model
public class ViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get;set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Edit(ViewModel customer)
{
using (AjaxSamplesEntities entities = new AjaxSamplesEntities())
{
Customers_Name primary = entities.Customers_Name.Where(x => x.Id == customer.Id).FirstOrDefault();
primary.Name = customer.Name;
entities.SaveChanges();
Customers_Country foreign = entities.Customers_Country.Where(x => x.CustomersId == customer.Id).FirstOrDefault();
foreign.Country = customer.Country;
entities.SaveChanges();
}
return RedirectToAction("Index", "Home");
}
}
View
@using (Html.BeginForm("Edit", "Home", FormMethod.Post))
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Screenshot
Output