Pass RouteValueDictionary to Html.Action as QueryString - asp.net-mvc

I have a view:
#{
RouteValueDictionary tRVD = new RouteValueDictionary(ViewContext.RouteData.Values);
foreach (string key in sQS.Split('&'))
{
var itemArr = key.Split('=');
tRVD[itemArr[0]] = itemArr[1];
}
tRVD["dtLastRequest"] = item.dtLastRequest;
tRVD["uiSubscription"] = item.uiSubscription;
tRVD["area"] = "";
}
#Html.Action("Count", "Search", tRVD)
Which generate RouteValueDictionary with 15 keys & values, and then pass it to Action "Count", controller "Search", as i could think?!
I dont know previous programmer, who wrote this code, but controller's action code is full or work with Request.QueryString, but right now, QueryString is empty.
Is it really possible to pass tRVD to Action "Count" as QueryString? Now, i could get values from:
RouteData.Values
there are my keys and values, but i dont want to change code of previous coder and rewrite all from QueryString to RouteData.

The query string is empty because the Count action method is really invoked not using an ordinary request, specifying the action's parameters in the URL, but directly within the framework, carrying the parameters in the RouteData object.
So you should access the parameters using RouteData, as you wrote.

Related

How does the Dart URI class QueryParameters handle Map values?

According to the documentation, it needs to follows the Form Post rules at: https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4. When looking at that information it did not give me much to work with in terms of complex objects or maps.
Right now, If I have a list for example: Each item in the list needs to be stringified.
var params = {"list": [1,2,3]};
// needs to be stringed.
params["list"] = params["list"].map((item)=>item.toString()).toList();
Simple. Also all base items need to be a string as well
var params = {"number": 1, "boolean": true};
params = params.forEach((k,v)=> params[k].toString());
But how do we handle maps?
var params = {"map": {"a":1,"b":"foo","c":false,"d":[]}};
// ??
It seems that after testing in my app and in dart pad, you need to make sure everything is strings, so i am trying to come up with a way to effectively cover lists, maps, and maybe more complex objects for encoding.
var params = {};
params["list"] = [1,2,3];
params["number"] = 1;
params["boolean"] = true;
params["map"] = {"a":1,"b":"foo","c":false,"d":[]};
params.forEach((String key, dynamic value){
if(value is List){
params[key] = value.map((v)=>v.toString()).toList();
}else if(value is Map){
// ????
}else{
params[key] = value.toString();
}
//maybe have an additional one for custom classes, but if they are being passed around they should already have their own JSON Parsing implementations.
}
Ideally, the result of this would be passed into:
Uri myUri = new Uri(queryParameters: params);
and right now, while i solved the list issue, it doesn't like receiving maps. Part of me just wanted to stringify the map as a whole, but i wasn't not sure if there was a better way. I know that when someone accidentally stringified the array, it was not giving me: ?id=1&id=2 but instead ?id=%5B1%2C2%5D which was not correct.
I don't think there is any special support for maps. Query parameters itself is a map from string to string or string to list-of-strings.
Everything else need to be brought into this format first before you can pass it as query parameter.
A simple approach would be to JSON encode the map and pass the resulting string as a single query parameter.

ASP MVC3 ActionLink not rendering variables

I am trying to use an ASP MVC3 action link to navigate to another view (same controller). The view is attached to a model that uses a compound key for its primary key. Below is the action link as it's written on the view
#Html.ActionLink("Edit Agent", "AgentEdit", "BankListMasterController",
new { #agentId = int.Parse(item.AgentId), #id = item.ID})
However when this is rendered it renders as the following
http://localhost:2574/BankListMaster/AgentEdit?Length=24
Which obviously throws an error:
The parameters dictionary contains a null entry for parameter 'agentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult AgentEdit(Int32, Int32)' in 'Monet.Controllers.BankListMasterController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Here is the controller method for good measure:
public ViewResult AgentEdit(int agentId, int id)
{
string compare = agentId.ToString();
BankListAgentId agent = (from c in db.BankListAgentId
where c.ID == id &&
c.AgentId.Equals(compare)
select c).Single();
return View("AgentEdit", agent);
}
#Html.ActionLink("Edit Agent", "AgentEdit", "BankListMasterController",
new { agentId = int.Parse(item.AgentId), id = item.ID}, null)
That should do the trick
And rationale is: as per http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.108).aspx
You won't find there method with (HtmlHelper, string, string, string, object) there's however (HtmlHelper, string, string, string, object, object) where the second last object is route values and the last are html attributes.
Based on the parameters you have provided, the wrong ActionLink is being called.
The problem is that it is "attempting to serialize a string object"
Here is the canonical answer to the "'Length' parameter in link" question:
Why does Html.ActionLink render "?Length=4"

Only first parameter value is getting while calling controller method using Url.action.

I am calling a controller method using Url.action like,
location.href = '#Url.Action("Display", "Customer", new { username = "abc",name = "abcdef",country = "India",email = "abc#hmail.com",phone = "9456974545"})';
My controller method is,
public void Display(string username, string name, string country, string email, string phone)
{ }
In this method, I can get only the value of first parameter (username). Its not getting other parameter values that is passed. All other values are null.
Please suggest me, whats wrong?
By default every content (which is not IHtmlString) emitted using a # block is automatically HTML encoded by Razor.
So, #Url.Action() is also get encoded and you are getting plain text. And & is encoded as &
If you dont want to Encode then you should use #Html.Raw(#Url.Action("","")).
The answer for you question is :
location.href = '#Html.Raw(#Url.Action("Display", "Customer", new { username = "abc",name = "abcdef",country = "India",email = "abc#hmail.com",phone = "9456974545"}))';
Hope this helps
There is a problem with '&' being encoded to the '& amp;'
model binder doesnt recognise this value. You need to prevent this encoding by rendering link with Html.Raw function.
Use '#Html.Raw(Url.Action......)'

How can I store a URL into a session variable in MVC and later read it in an action method?

I have the following on a razor page:
#{ Session["CurrentUrl"] = Request.Url.ToString(); }
I then call another page and in the POST action method of that second page I try to check the value of
var aaaa = Session["CurrentUrl"];
Can someone give me some advice as to why the value of variable aaaa is null ?
You just need to store your data in TempData variable. TempData internally stores in session object.
in C# Code:
TempData["CurrentUrl"] = Request.Url.ToString();
in Razor View:
#{ string url = TempData["CurrentUrl"].ToString(); }
Hope this helps!

get parameters from referrer

Is there any easy way to extract the parameters of the referrer url as contained in Request.UrlReferrer? Is there another way to get the parameters used by the referrer?
Query?blahID=3&name=blah
I am refering to getting blahID and name from the url. It can be done with a bunch of string manipulations, but was hoping there was an easier way.
Use HttpUtility.ParseQueryString from System.Web. Something like this should work:
string blahID = string.Empty;
if(Request.UrlReferrer != null)
{
var q = HttpUtility.ParseQueryString(Request.UrlReferrer.Query);
blahID = q["blahID"];
}

Resources