ASP MVC3 ActionLink not rendering variables - asp.net-mvc

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"

Related

named parameter in Dart

Good day, I am trying to apply named parameter {num phone} to the below example:
main(){
showInfo("abc#mail.com", "Fatima");
}
String showInfo(String email, String name, {num phone}) {
print(email);
print(name);
print(phone);
return "$email : $name : $phone";
}
but I receive the error:
Error: The parameter 'phone' can't have a value of 'null' because of its type 'num', but the implicit
default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
Future<String> showInfo(String email, String name, {num phone}) async {
^^^^^
your help is appreciated.
You marked the parameter as a num, which means it cannot be null. However, the default for named parameters that are not used is null, so you cannot have a named parameter that is optional with a default value of null with a datatype that does not accept null.
One option is to give it a default value other than null:
String showInfo(String email, String name, {num phone = 0})
Another option is to make it a named, but required parameter, so it will never get a default value:
String showInfo(String email, String name, {required num phone})
Another alternative is to actually keep the phone optional:
String showInfo(String email, String name, {num? phone})
Some additional wisdom: phone numbers can start with leading zeroes that are important and should not be deleted on saving it. You cannot use num for a phone number, you will have to use string.
The error is pretty clear, you have to pass a value to the phone parameter or add a default value. This is due to the introduction of Null Safety in Dart 2.12.
You can pass a value like this :
showInfo("abc#mail.com", "Fatima", phone: 0);
Or add a default value to the phone parameter :
String showInfo(String email, String name, {num phone = 0}) {
print(email);
print(name);
print(phone);
return "$email : $name : $phone";
}
If you want, you can use Dart <2.12 to avoid Null Safety, your code should work properly then.

Argument of '#selector' does not refer to an initializer or method in

I update my project from Swift2.2 to Swift3.0 But "Argument of '#selector' does not refer to an initializer or method" issue received.
Here is code :
for object in Students {
let sectionNumber = collation.section(for: object.firstName!, collationStringSelector: #selector(NSObjectProtocol.self))
sections[sections.count - 1 - sectionNumber].append(object)
}
class Person: NSObject {
#objc var name: String
init(name: String) {
self.name = name
}
}
let p = Person(name: "Alice")
let collation = UILocalizedIndexedCollation.current()
collation.section(for: p, collationStringSelector: #selector(getter: Person.name))
This is also fine since Selector is from Objective-C. Which we need to :NSObject and #objc.
As per docs
func section(for object: Any, collationStringSelector selector: selector) -> Int
Description
Returns an integer identifying the section in which a model object belongs.
The table-view controller should iterate through all model objects for the table view and call this method for each object. If the application provides a Localizable.strings file for the current language preference, the indexed-collation object localizes each string returned by the method identified by selector. It uses this localized name when collating titles. The controller should use the returned integer to identify a local “section” array in which it should insert object.
Parameters
object
A model object of the application that is part of the data model for the table view.
*selector*
A selector that identifies a method returning an identifying string for object that is used in collation. The method should take no arguments and return an NSString object. For example, this could be a name property on the object.
Returns
An integer that identifies the section in which the model object belongs. The numbers returned indicate a sequential ordering.
Solution
Change like below
collation.section(for: "test", collationStringSelector: #selector(getStr)) //here getStr is an other function returning String
func getStr()->String{
return "test"; // this should return an identifying string for object that is used in your collation
}
I implement user2215977 answer but app crash again & again. Now i just change the #selector(NSObjectProtocol.self) to "self". All error gone but just one warning received " Use of string literal for Objective-C selectors is deprecated; use #selector instead ".
If any person have idea to resolve this warning then share me.Otherwise error go now.

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......)'

Pass RouteValueDictionary to Html.Action as QueryString

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.

ASP MVC Model Binder

I can't explain Model Binder behavior in this example:
enum Gender
{
Male,
Female
}
#model Gender
#Html.ActionLink("Test", "Index", new { gender = Model });
public ActionResult(string gender)
{
ViewBag.Gender = gender;
return View()
}
why it works — string gender argument in action correct get Enum type?
It works because absolutely everything can be converted to a string by way of object.ToString(). Further, it happens on Enum that the conversion produces something useful; the text of the enumeration value's name.
The default binder can convert back and forth between these values, plus the numeric ones, in most cases.

Resources