In MVC3, How can I pass multiple values to a querystring parameter by ActionLink? - asp.net-mvc-3

In my MVC3 project I have a controller "Test" with this method for Index action
Function Index(fields() As String) As ViewResult
...
Then on the View I have a a multi-select drop down list
#Html.ListBox("fields", New MultiSelectList(my_list, "Value", "Text"))
and everything works fine, however when I try to create an Actionlink passing multiple values of parameter "fields" in the querystring like this
#Html.ActionLink("TestLink", "Index", New With {
.fields = "value1&fields=value2"})
I get the following broken link in the HTML source with the querystring encoded
TestLink
How can I pass multiple values to a querystring parameter by ActionLink?

If you want to do this dynamically by fetching the values from the listbox simply put the listbox inside a form whose action is pointing the controller action you want to invoke and then use a submit button. HTML will then do the job for you.
If you want to generate an ActionLink you could use the following:
#Html.ActionLink(
"TestLink",
"Index",
New RouteValueDictionary() From {
{ "fields[0]", "value1" },
{ "fields[1]", "value2" }
}
)

Related

how to pass parameters to controller from view

I need to pass parameters from a view to control in a form. These parameters are for example a string that is not in a textField. The string is "FATHER" and I send this string from the view to the controller when I click on the submit button in the form. Anyone can suggest how I can do it?
add a hidden input field to your form with a value of "FATHER":
<?= form_hidden('name_of_field', 'FATHER') ?>
and in your controller, get the value when the form is submitted:
$value = $this->input->post('name_of_field');
So if this question is about asp.net MVC ... :
Are you using a strongly typed view, do you have a ViewModel? If so, add a property of type string, say StringToHide, to it, set it to "FATHER" and then, in your form, add
#Html.HiddenFor(StringToHide)
Then the information will be passed to the controller, but it is not editable by the user. And if you're not using a viewmodel yet, well, then you should. It's a clean way to compose all your data when passing from views to controllers and vice versa.
Assuming Asp.net MVC
In View
#{ Html.RenderAction("Menu", "", new { parameterFromView= "FATHER" }); }
In Controller
[HttpPost]
public ActionResult Menu(string parameterFromView)
{
// do whatever
}
In jquery call append the text Father to the URL and accept it in controller.
see code below.
$("#submit").click(funtion(){
window.url = "Controller/Action/" + "Father";
});

Make link Ajax.ActionLink in MVC 4

Beeing new to Ajax i need help with how to add the querystring and invoke the Index action without a postback. Now the link looks like this:
#sectionGroup.Term, #sectionGroup.Count
My guess is using Ajax.ActionLink but how do i create the querystring?
Everything that you add to the routeValues and doesn't match a route segment will be added to the querystring.
Using your example, it would look something like this with Ajax.ActionLink:
#Ajax.ActionLink(
sectionGroup.Term + ", " + sectionGroup.Count,
"Index",
new { section = sectionGroup.Term },
new AjaxOptions { UpdateTargetId = "id-of-container" }
)
In your Index action you need to return a partial view when it is requested with ajax. This will remove the layout (master page) from the response.
if (Request.IsAjaxRequest())
return PartialView(model);
return View(model);
Remember to add a reference to the jquery.unobtrusive-ajax.js file.

Add jQuery Mobile transition to ASP.Net MVC Form

How does one add a jQuery Mobile transition to an HTML POST rendered with the ASP.Net MVC Html.BeginForm helper?
The transition requires an HTML attribute data-transition be added (I think to the form tag, but the docs are unclear on that point, providing only a hyperlink example).
I'm trying to use the BeginForm overload to add attributes to the rendered form tag. When using the new { ... } syntax to declare an anonymous class representing the HTML attributes, I get an error if an attribute name has a dash in it.
using (Html.BeginForm("Login", "Account", FormMethod.Post,
new { data-transition="pop" }))
Error: Invalid anonymous type member declarator
This, in spite of the fact that the MSDN documentation shows an attribute with a dash in the name
new { id = "text1", accept-charset="iso-8859-1" }
Create a dictionary:
using (Html.BeginForm("Login", "Account", FormMethod.Post,
new Dictionary<string, object>{{ "data-transition", "pop" }} ))
If you prefer to use an anonymous object to specify your attributes then you could do the following
using (Html.BeginForm("Login", "Account", FormMethod.Post, new { data_transition = "pop" } ))
In short you replace the hypen with an underscore

Hyperlink to call action method

I'm trying to figure out a trivial creation of a hyperlink (either through using an Html.ActionLink or plain hyperlink markup) to call a certain controller action and pass in a userId
In have a controller with an Index action. I have a hyperlink in another unrelated view where I need to all that controller's index action and pass in a userId which is param in the action method.
Update:
Html.ActionLink("Test Something", "Index", "Tests", new { #userId = #Model.User.UserId }, null)
that is an example of what I tried. Problem is, it's not showing Tests/ in the url when I moue over the url during runtime
You could use the ActionLink helper from any view anywhere in the application:
#Html.ActionLink(
"call index", // linkText
"index", // action
"home", // controller
new { userid = "123" }, // routeValue
null // htmlAttributes
)
Assuming default routes this should generate:
call index

Html.ActionLink equivalent of a specific URL format

I want to use Html.ActionLink to return an anchor tag with href like /Product/22/Product-Name, where Product is my controller name and 22 is my id and Product-Name is my title.
In my global.asax, I have set the following route :
routes.MapRoute(
"ProductRoute", // Route name
"Product/{id}/{title}", // URL with parameters
new { controller = "Product", action = "Index", id = UrlParameter.Optional });
I tried using following code
#Html.ActionLink(model.Name, null, "Product",
new { id = model.Id, title = model.Name.Replace(" ","-") }, null)
but it yields Product/Browse/22?title=Product-Name. This code is in Browse View returned by Browse action of Catalog controller and that's why it picks up the Browse in between.
Strangely, the same code works fine in the Index View returned by Index action of the Home Controller. What's the reason for this difference? . Am I missing something or this is a bug in Asp.net MVC3.
What Should I do to achieve my stated URL format. For the meantime, I am using this code
#product.Name
make sure you have defined the custom route before the default route and try
#Html.ActionLink(model.Name,
"Index",
new {controller="Product", id = model.Id, title = model.Name.Replace(" ","-") }, null)
the first parameter is the DisplayName, second the ActionResult's name and then the RouteValues

Resources