namespace it13961223.Controllers
{
public class kharidsController : Controller
{
private anbarentitiescontext db = new anbarentitiescontext();
private anbarentitiescontext db1 = new anbarentitiescontext();
public ActionResult Create([Bind(Include = "kharidID,radif,code_piece,name_piece,model,tedad,Date,tedad_mande,tozih")] kharid kharid)
{
if (ModelState.IsValid)
{
return View(kharid);
}
public ActionResult Create([Bind(Include = "khtID,radif,code_piece,name_piece,model,tedad,Date,tedad_mande,tozih")] kht kht)
{
if (ModelState.IsValid)
{
db1.khts.Add(kht);
db1.SaveChanges();
return View();
}
return View();
}
/************/
public class anbarentitiescontext:DbContext
{
public DbSet<kharid> kharids { get; set; }
public DbSet<kht> khts { get; set; }
}
/******************************/
when i run my project .show error:
The current request for action 'Create' on controller type 'kharidsController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type it13961223.Controllers.kharidsController
System.Web.Mvc.ActionResult Create(Models.kht) on type it13961223.Controllers.kharidsController
3 then post to the Index action method and save these two tables into your database;
public ActionResult Index([Bind(Prefix= "Student")] Student Student,[Bind(Prefix= "Teacher")] Teacher Teacher){
ApplicationDbContext db = new ApplicationDbContext();
db.Students.Add(Student);
db.Teachers.Add(Teacher);
db.SaveChanges();
}
Welcome to back if met any question :)
With regards, Angelina Jolie
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
1 Points
4 Posts
problem:asp.net mvc
Mar 14, 2018 12:58 PM|aabedeni056|LINK
hello . i creat 1 project in asp.net mvs.
i have 3 table .
i creat 1 controler , model , view for 1 table .
i want save all fields in create form in 2 table in the same time .
please help me ,
how i must change action create for save. change in 2 table ....
thank you
All-Star
30731 Points
11185 Posts
Re: problem:asp.net mvc
Mar 14, 2018 02:09 PM|mgebhard|LINK
This site has Getting Started tutorials that will step you through the process of creating a basic MVC project and working with data. Start here...
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/
Member
3 Points
18 Posts
Re: problem:asp.net mvc
Mar 14, 2018 03:03 PM|arunZer0|LINK
Simple solution is, Add all the object in its own Dbset and then save both object using same dB context with single SaveChanges(); method.
Arun G
Member
1 Points
4 Posts
Re: problem:asp.net mvc
Mar 15, 2018 06:01 AM|aabedeni056|LINK
hello
thank you
my code in this project is under :
namespace it13961223.Controllers
{
public class kharidsController : Controller
{
private anbarentitiescontext db = new anbarentitiescontext();
private anbarentitiescontext db1 = new anbarentitiescontext();
public ActionResult Create([Bind(Include = "kharidID,radif,code_piece,name_piece,model,tedad,Date,tedad_mande,tozih")] kharid kharid)
{
if (ModelState.IsValid)
{
db.kharids.Add(kharid);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(kharid);
}
public ActionResult Create([Bind(Include = "khtID,radif,code_piece,name_piece,model,tedad,Date,tedad_mande,tozih")] kht kht)
{
if (ModelState.IsValid)
{
db1.khts.Add(kht);
db1.SaveChanges();
return View();
}
return View();
}
/************/
public class anbarentitiescontext:DbContext
{
public DbSet<kharid> kharids { get; set; }
public DbSet<kht> khts { get; set; }
}
/******************************/
when i run my project .show error:
The current request for action 'Create' on controller type 'kharidsController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type it13961223.Controllers.kharidsController
System.Web.Mvc.ActionResult Create(Models.kht) on type it13961223.Controllers.kharidsController
please help me .
Contributor
5200 Points
2307 Posts
Re: problem:asp.net mvc
Mar 15, 2018 06:22 AM|AngelinaJolie|LINK
Hi aabedeni056,
Sir, it is easy to do in MVC, please refer to
1 add a new view table named PersonView
2 on view page, it looks like
3 then post to the Index action method and save these two tables into your database;
public ActionResult Index([Bind(Prefix= "Student")] Student Student,[Bind(Prefix= "Teacher")] Teacher Teacher){ ApplicationDbContext db = new ApplicationDbContext(); db.Students.Add(Student); db.Teachers.Add(Teacher); db.SaveChanges(); }
Welcome to back if met any question :)
With regards, Angelina Jolie
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.