ASP.NET MVC additions and deletions to check the drop-down box to change the binding

Development tools and key technologies: VS MVC

Author: Chen Chi Fan

Write Time: 2019.4.15

Inquire:

public ActionResult SelectById(int Id)

        {

            try

            {

 SYS_  sys = (from tb in myModel.SYS_              

 where tb. ID == Id                                        

select tb).Single();

                return Json(sys, JsonRequestBehavior.AllowGet);

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                return Json("", JsonRequestBehavior.AllowGet);

            }

        }

Add:

public ActionResult Insert (SYS_  sys)

        {

            ReturnJson returnJson = new ReturnJson();           

            try

            {

                    if (oldCount == 0)

                    {

                      myModel.SYS_.Add (sys);

                        if (myModel.SaveChanges() > 0)

                        {

                            returnJson.State = true;

                            returnJson.Text = "";

                        }

                        else

                        {

                            returnJson.State = false;

                            returnJson.Text = "";

                        }

                    }

            catch (Exception e)

            {

                Console.WriteLine(e);

                returnJson.State = false;

                returnJson.Text = "";

            }

            return Json(returnJson, JsonRequestBehavior.AllowGet);

        }

    modify:

    public ActionResult Update (SYS_ sys)

        {

            ReturnJson returnJson = new ReturnJson();

            try

            {

                    if (myModel.SaveChanges() > 0)

                    {

                        returnJson.State = true;

                        returnJson.Text = "";

                    }

                    else

                    {

                        returnJson.State = false;

                        returnJson.Text = "";

                    }

            catch (Exception e)

            {

                Console.WriteLine(e);

                returnJson.State = false;

                = returnJson.Text " abnormal data!" ;

            }

            return Json(returnJson, JsonRequestBehavior.AllowGet);

        }

delete:

public ActionResult Delete (int  Id)

        {

            ReturnJson returnJson = new ReturnJson();

            try

            {

                int intRow = (from tb in myModel.SYS_

                                   where tb.ID == Id

                                   select tb).Count();

                if (intRow > 0)

                {

                    returnJson.State = false;

                    returnJson.Text = "";

                    return Json(returnJson, JsonRequestBehavior.AllowGet);

                }                          

Binding drop-down box:

public ActionResult Select(int ID)

         {

 

            List<Select> list= (from tb in myModels.SYS_

                                      where tb.ID == ID

                                      select new Select

                                      {

                                          id = tb.ID,

                                          text = tb.Name

                                      }).ToList();

            return Json(list, JsonRequestBehavior.AllowGet);

        }

Conclusion: For drop-down box bound basically covering all go, remain the same.

Guess you like

Origin blog.csdn.net/qq_44554890/article/details/89293327