Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

AspNetCore: Forward an asp action to another action including the id

26.04.2018 (👁4506)


 

How can you pass the current action on to another action in an asp controller?

If I have done my memory operation in the Controller: Notes, then I would like to forward the output to the View: Details.

 

Solution:

For this one uses the RedirectToAction

The RedirectToAction has optionally 3 parameters: action name, controller name and reference parameter like the ID

 

Forwarding of Action Edit / 81 to Controller Action Notes / Details / 81

 

Software: Asp.Net Core 2 MVC

 

Example Code:

Forwarding with Action, Controller, ID

return RedirectToAction("details","Notes",  new { id = IDNote } );

 

 

 

 

Code reference

Asp.Net Core 2 MVC

 

Forwarding of Action Edit / 81 to Controller Action Notes / Details / 81

       // POST: Notes/Edit/5

        [HttpPost]

        [ValidateAntiForgeryToken]

        [Authorize]

        public async Task<IActionResult> Edit(long ID)

        {

            // -------------< Edit_Postback() > -------------

            IFormCollection form = Request.Form;

 

            //*from form

            string sIDNote = form["Note.IDNote"];

 

            //< check: Form[ID] >

            if (sIDNote == null) { return Content("IDNote missing"); }

            long IDNote = 0;

            try

            {

                IDNote = System.Convert.ToInt64(sIDNote);

            }

            catch (Exception )

            {

                return Content("ID not type long");

            }

            //</ check: Form[ID]  >

 

            //< check: Path_ID >

            if (ID==0)

            {

                //< ID=0  >

                ID = IDNote;

                //</ ID=0  >

            }

            else

            {

                //< with ID  >

                if (ID != IDNote) { return NotFound(); }

                //</ with ID  >               

            }

 

            //</ check: Path_ID >

 

 

 

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDUser = await UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

            //--</ Get User ID >--

 

            //< get_database >

            NoteModel note;

            if (IDNote > 0) {

 

                note = _dbContext.tbl_Notes.SingleOrDefault(n => n.IDNote == IDNote);

                if (note==null) { return Content("No Record found for ID=" + IDNote); }

 

                //< check Owner >

                long IDOwner = note.IDUser;

                if (IDOwner != IDUser) { return Content("You are not the Owner"); }

                //</ check Owner >

            }

            else

            {

                note = new NoteModel();

                note.IDUser = IDUser;

            }

            //</ get_database >

 

 

            //----< Save Note-Data >----

            //< get >

            string sHTML = form["ctlEditor_Text"];

            string sText= Html_Methods.HTML_to_Text(sHTML);

            string sTitle = form["Note.Title"];

            string sFolder = form["Note.Folder"];

            //</ get >

 

 

 

            //< correct >

            //*SQL injection, script blocks..

            sHTML = Correct_Methods.correct_In_String_Minimal(sHTML);

 

            sTitle = Correct_Methods.correct_In_String_Strong(sTitle);

            sFolder = Correct_Methods.correct_In_String_Strong(sFolder);

            //</ correct >

 

            //--< data >--

            note.Title = sTitle;

            note.HTML    = sHTML;

            note.Text = sText;

            note.Folder = sFolder;

            note.DtEdit = DateTime.Now;

            note.IsDraft = false;   //try allways on

            //--</ data >--

 

            try

            {

                if(IDNote>0)

                {

                    //< update Server >

                    _dbContext.Update(note);

                    //</ update Server >

                }

                else

                {

                    //< Add on Server >

                    _dbContext.tbl_Notes.Add(note);

                    _dbContext.SaveChanges();

                    IDNote = note.IDNote;

                    //</ Add on Server >

                }

                //await _dbContext.SaveChangesAsync();

            }

            catch (DbUpdateConcurrencyException)

            {

                return Content("Error in saving Note with ID=" + IDNote );

            }

            //----</ Save Note-Data >----

 

 

            //----< Delete Images not in HTML >----

            List<Note_Image_Model> list_Images = _dbContext.tbl_Notes_Images.Where(img => img.IDNote == IDNote).ToList();

            foreach (Note_Image_Model image in list_Images)

            {

                string sImage_BaseTag = "Image_" + IDNote + "_" + image.ImageNr;

                if (sHTML.IndexOf(sImage_BaseTag) < 0)

                {

 

                    //< delete Image_sized >

                    //--< Image >--

                    string folder_Path_Images = _hostingEnvironment.WebRootPath + "\\User_Files\\Notes\\Images\\";

                    File_Methods.Delete_File(folder_Path_Images + "\\" + sImage_BaseTag + "_mini.jpg");

                    File_Methods.Delete_File(folder_Path_Images + "\\" + sImage_BaseTag + "_pad.jpg");

                    File_Methods.Delete_File(folder_Path_Images + "\\" + sImage_BaseTag + "_blog.jpg");

                    File_Methods.Delete_File(folder_Path_Images + "\\" + sImage_BaseTag + ".jpg");

                    //</ delete Image_sized >

 

                    _dbContext.tbl_Notes_Images.Remove(image);

                }

            }

            //----</ Delete Images not in HTML >----

 

 

            //< save note+images >

            await _dbContext.SaveChangesAsync();

            //</ save note+images >

 

            //< load Output-Data >

            Notes_Edit_DataModel data = new Notes_Edit_DataModel();

            data.Note = note;

            //</ load Output-Data >

 

            //-< target >--

            string sGoto=Request_Methods.get_Query_Parameter(Request, "gt");

            if(sGoto == "") {

                //< out to view >

                string sMobile_Extenstion = Request_Methods.set_optional_Mobile_Extension(Request);

                return View("edit" + sMobile_Extenstion, data);

                //</ out to view >

            }

            else

            {

                return RedirectToAction("details","Notes",  new { id = IDNote } );                 //,"Notes","ID=" + IDNote

            }

            //-</ target >--

 

 

            // -------------</ Edit_Postback() > -------------

        }