0

I have an controller where I have two standard ActionResults that concerns the scenario: Index and Details In details I check various conditions about the user identity and I check for an existing amount of credits available in his profile. If all criteria are meet I redirect to Details view, and update/add some records . What I want is that before the effective redirection (and of course update/add process) to fire an notification (something like: "Hey user X, you have xx credits and for completing you will consume yy credits. Are you sure that you want to continue y/n"). If no stay on current Index View, if yes go to Details view and in Details ActionResult do all the update/insert stuff. In Index view I use standard ActionLink to navigate to Detail View:

@Html.ActionLink(" View this Candidate details ...", "Details", "Candidates", new { id = item.IdCV }, new { @class = "btn btn-default fa fa-link" })

and the action Details in controller is :

    // GET: Candidates/Details/5
    // Restrict by role:
    //[Authorize(Roles = "Admin, Candidate, Employer")]
    public ActionResult Details(int? id)
    {
        if (User.Identity.IsAuthenticated)
        {
            //obtaining the user profile info
            var jobs = new Jobs();
            var currentuser = User.Identity.GetUserId();//UserManager.FindById(User.Identity.GetUserId());
            JobShopEntities db = new JobShopEntities();
            var TheUser = db.AspNetUsers.Where(u => u.Id == currentuser)
                         .Select(u => new
                         {
                             ID = u.Id,
                             Email = u.Email,
                             Username = u.UserName,
                             Surrname = u.Surname,
                             Name = u.Name,
                             Role = u.Role,
                             CreditBalance = u.CreditBalance
                         }).Single();

            var idul = TheUser.ID;
            var username = TheUser.Username;
            var name = TheUser.Name;
            var surrname = TheUser.Surrname;
            var credit = TheUser.CreditBalance;
            var CreditUsed = 5;
            var NewCredit = credit - CreditUsed; //This value I want to update in AspNetUsers.CreditBalance field 
            var role = TheUser.Role;
            var operatia = "Display CV detail";
            int? cvul = id;
            int? jobul = null;

            //testing the user
            if (role == 3)
            {
                //if role is ok, testing the credit balance
                if (credit > 10)
                {
                    if (id == null)
                    {
                        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                    }
                    Candidate candidate = db.Candidate.Find(id);
                    if (candidate == null)
                    {
                        return HttpNotFound();
                    }
                    //update credits in AspNetUsers.CreditBalance
                    UpdateCredit(idul, username, NewCredit);

                    //save in audit journal CreditJournal.CreditsUsed
                    SaveAudit(operatia, idul, cvul, jobul, CreditUsed);
                    return View(candidate);
                }
                else
                {
                    //redirect to something
                    return RedirectToAction("NoCredit", "Jobs");
                }

            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            return RedirectToAction("Login", "Account");
        }
    }

The update and insert methods that are concerned are:

UpdateCredit(idul, username, NewCredit); 

and:

SaveAudit(operatia, idul, cvul, jobul, CreditUsed);

Also I have a similar demand before entering a Create View from main menu:

@Html.ActionLink("Add a new Job", "New", "Jobs")

Here also I check for (nearly) same conditions in New ActionResult (OK, here are the two flavors of ActionResult: New() and [HttpPost] New() )

L.E.

Finally I found a simple way that it works but with some issues:

@Html.ActionLink(" Detalii ...", "Details", "Candidates", new { id = item.IdCV }, new { onclick="return confirm('For viewing candidate details you will consume 5 credits from x available!
Do you want to continue??')", @class = "btn btn-default fa fa-link" })

respectively

@Html.ActionLink("New Job", "New", "Jobs", new { onclick="return confirm('For adding a new job you will consume 5 credits from x available!
Do you want to continue?')"})

For both I want a way for formatting the message (for example the
that I inserted, have no effect) and to inject in message text values for credit as they are in Controller.

Anyway the first example works, but the second has an issue: the link is modified with an parameter (for a not know reason) from:

http://localhost:23442/Jobs/New

to:

http://localhost:23442/Home/New?Length=4

net4u
  • 43
  • 9
  • 1
    Long story short, you want to prompt the user before sending a request to `Details` action. This is a simple [example](https://dotnetfiddle.net/ujNp1w). – lbrahim Jun 26 '15 at 10:42
  • @ibrahim mainly is what I want to mimic, excepting that I don't want on pageload, but un ActionLink click as here: [link](https://dotnetfiddle.net/a2BgD8) and also I want to pull some values from Details (credit, CreditUsed) and instead to display JSON alert to continue with code. – net4u Jun 26 '15 at 11:33
  • 1
    You can get rid of the query parameter - see [this answer](http://stackoverflow.com/questions/824279/why-does-html-actionlink-render-length-4) – levelnis Jun 26 '15 at 23:00

0 Answers0