简体   繁体   中英

ASP.NET MVC3 C# - when user clicks button, change record in DB from 1 to 2 and viceversa

Basically a button functionality the changes the data in a record in a database.

Pseudo:

If record in DB = 1, on button click, change to 2 Else if record in DB = 2, on button click, change to 1

Thank in advance for any tips/help

Amy you can edit your question and post the code there and apply proper formatting.

Since it is your 1st web app and it is tips/ help you are looking for :

  1. http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part1-cs
  2. http://weblogs.asp.net/scottgu/archive/2011/03/09/free-video-training-asp-net-mvc-3-features.aspx
  3. http://msdn.microsoft.com/en-us/library/gg416514(v=vs.98).aspx

These links will help you understand web-apps and MVC3 better - Come back here with any question you got after trying out these tutorials.

Sudo controller action.

public ActionResult DetailsChanged(DetailsRepository detailsRepository, Details detailsModel)
{
  if(!ModelState.Valid)
{
   ViewData["Error"] = "Error";
   return View();
}

Details newDetails = detailsRepository.FirstOrDefault(x => x.ID == detailsModel.Id);
if(newDetails != null)
{
   if(newDetails.Id == 1)
   {
      newDetails.Id = 2;
   } else {
      newDetails.Id = 2;
   }
   detailsRepository.SaveChanges();
   return View();
 }
}

Something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM