0
@Html.ActionLink((string)Model[i]["name"], "Info", "Home", new { uid = Model[i]["uid"].ToString() }, null)  

[HttpGet]
            public ActionResult Info(string uid)
            {
                TempData["uid"] = uid;
                SearchModel model = new SearchModel();
                List<DataTable> tables = model.GetUserInfo(uid);
                TempData["tables"] = tables;

                return View(model);
            }

In the controller, there is a method that returns JsonResult. This method is used to obtain data, on the basis of which the js code will be executed.

public JsonResult GetCountryRisk(SearchModel model)
    {
        string uid = TempData["uid"].ToString();
        IEnumerable<CountryRisk> list = new List<CountryRisk>();
        list = model.GetCountryRisk(uid);
        TempData["uid"] = uid;
        return Json(list, JsonRequestBehavior.AllowGet);
    }

The view has elements that simulate tabs and when clicked on the selected item, Ajax is called, which receives the data and passes it to the script.

    <div id="tabHeadDiv">
        <input type="button" value="Summary" class="tabControl" id="1" onclick="ShowOrHideTabs(this.id)" />
        @Ajax.ActionLink("Relations", "Info", null, new AjaxOptions { Url = Url.Action("GetDataForGraph"), OnSuccess = "DrawGraph" }, new { @id = "2", @class = "tabControl" })
        @Ajax.ActionLink("Map", "Info", null, new AjaxOptions { Url = Url.Action("GetCountryRisk"), OnSuccess = "drawRegionsMap" }, new { @id = "3", @class = "tabControl" })
    </div>

Problem is that if a user opens several links in a new tab and wants to go to the tab, then on all tabs the result will be displayed, which was executed last. So I want to understand if it's possible to send parameter to the GetCountryRisk method without using TempData ["uid"].

3
  • 1
    Make uid a part of the search model and pass it as a param in links Commented Mar 5, 2018 at 12:59
  • @Andrei Could you explain it little more? As I understood, I need to pass it in routeValues? I tried to do like @Ajax.ActionLink("Map", "Info", new { tUid = ViewBag.Main.Rows[0]["uid"]}, new AjaxOptions { Url = Url.Action("GetCountryRisk"), OnSuccess = "drawRegionsMap" }, new { @id = "3", @class = "tabControl" }) but in controllers it return null. Commented Mar 5, 2018 at 15:26
  • Expanded in the answer Commented Mar 5, 2018 at 15:49

1 Answer 1

1

First, stop using a ViewBag or a TempData for uid. This is clearly a part of your state, and while these key-value stores have their uses, they are no good for your case.

Make uid a part of SearchModel, as it is obviously important for you as you handle search requests, and in Info action do this:

SearchModel model = new SearchModel();
model.UID = uid;

Now on the page I don't really understand why the Ajax.ActionLink mentions one controller/action pair and then uses another as the URL. Here is how I would imagine it:

@Ajax.ActionLink(
    "Your controller name",
    "GetCountryRisk",
    new {uid = Model.UID},
    new AjaxOptions {OnSuccess = "drawRegionsMap" },
    new { @id = "3", @class = "tabControl" })

Finally in the GetCountryRisk action just use uid:

public JsonResult GetCountryRisk(SearchModel model)
{
    string uid = model.UID;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that problem solved. I accept your answer as solution. But could you then explain how I can whithout TempData cache data which i get from first completing that Json? Because of user can swith from "tabs" and each time method would genereate Json again.
@IhorMykytiv, that depends. TempData is not a proper way to keep bulky stuff cached anyways. If it does not take a huge amount of time to generate the JSON, I see no problem creating it all over again for each request.
Problem is that user may switch tabs as many time as he want. Second things that method may return big amount of data from db (for example it may executing up for 5-10 seconds) and if user each time will wait, it would be not well. So I need find way to evaid that time. Anyway thanks you for your patience :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.