I have an action link that when a user clicks on it, it redirects to an mvc view, the actionlink is below,
<%=Html.ActionLink("Select", "Review?usrItId=" + drResponse["ItineraryId"].ToString() + "&Type=" + drResponse["FareType"].ToString(), "", new { #class = "fCheck" })%>
but when the user clicks on it i get the below error,
system.Web.HttpException: A potentially dangerous Request.Path value was detected from the client (?)
the HTML is presented like this:
<a class="fCheck" href="/controller/Review%3fusrItId%3dsi1000%26Type%3dNoFrills?Length=0">Select</a>
thanks in advance for the help. I am using MVC 3, .NET 3.5
The query parameters (userItId and Type here) need to be specified in a different way. It is what the routeValues argument of ActionLink is for:
<%=Html.ActionLink("Select", "Review", new { usrItId = drResponse["ItineraryId"].ToString(), Type = drResponse["FareType"].ToString() }, new { #class = "fCheck" })%>
Try to change your action link to this:
#Html.ActionLink("Select", "Review",
new { usrItId = drResponse["ItineraryId"].ToString(), Type = drResponse["FareType"].ToString()},
new {#class = "fCheck"})
Related
I'm creating a form for a DropDown like this:
#{
Html.BeginForm("View", "Stations", FormMethod.Get);
}
#Html.DropDownList("id", new SelectList(ViewBag.Stations, "Id", "Name"), new { onchange = "this.form.submit();" })
#{
Html.EndForm();
}
If I choose a value from my dropdown I get redirected to the correct controller but the URL is not as I would like to have it:
/Stations/View?id=f2cecc62-7c8c-498d-b6b6-60d48a862c1c
What I want is:
/Stations/View/f2cecc62-7c8c-498d-b6b6-60d48a862c1c
So how do I get the id= querystring parameter replaced by the more simple URL Scheme I want?
A form with FormMethod.Get will always post back the values of its form controls as query string values. A browser cannot generate a url based on your route configurations because they are server side code.
If you really wanted to generate /Stations/View/f2cecc62-7c8c-498d-b6b6-60d48a862c1c, then you could use javascript/jquery to build your own url and redirect
#using (Html.BeginForm("View", "Stations", FormMethod.Get))
{
#Html.DropDownList("id", new SelectList(ViewBag.Stations, "Id", "Name"))
}
var baseUrl = '#Url.Action("View", "Stations")';
$('#id').change(function() {
location.href = baseUrl + '/' $(this).val();
});
Side note: Submitting on the .change() event is not expected behavior and is confusing to a user. Recommend you add a button to let the user make their selection, check it and then submit the form (handle the button's .click() event rather that the dropdownlist's .change() event)
Remove "id"
from
#Html.DropDownList("id", new SelectList(ViewBag.Stations, "Id", "Name"), new { onchange = "this.form.submit();" })
I'm creating a form for a DropDown like this:
#{
Html.BeginForm("View", "Stations", FormMethod.Get);
}
#Html.DropDownList("id", new SelectList(ViewBag.Stations, "Id", "Name"), new { onchange = "this.form.submit();" })
#{
Html.EndForm();
}
If I choose a value from my dropdown I get redirected to the correct controller but the URL is not as I would like to have it:
/Stations/View?id=f2cecc62-7c8c-498d-b6b6-60d48a862c1c
What I want is:
/Stations/View/f2cecc62-7c8c-498d-b6b6-60d48a862c1c
So how do I get the id= querystring parameter replaced by the more simple URL Scheme I want?
A form with FormMethod.Get will always post back the values of its form controls as query string values. A browser cannot generate a url based on your route configurations because they are server side code.
If you really wanted to generate /Stations/View/f2cecc62-7c8c-498d-b6b6-60d48a862c1c, then you could use javascript/jquery to build your own url and redirect
#using (Html.BeginForm("View", "Stations", FormMethod.Get))
{
#Html.DropDownList("id", new SelectList(ViewBag.Stations, "Id", "Name"))
}
var baseUrl = '#Url.Action("View", "Stations")';
$('#id').change(function() {
location.href = baseUrl + '/' $(this).val();
});
Side note: Submitting on the .change() event is not expected behavior and is confusing to a user. Recommend you add a button to let the user make their selection, check it and then submit the form (handle the button's .click() event rather that the dropdownlist's .change() event)
Remove "id"
from
#Html.DropDownList("id", new SelectList(ViewBag.Stations, "Id", "Name"), new { onchange = "this.form.submit();" })
I would like to use a javascript library that hits the data- attribute in the link i have created.
#model IEnumerable<string>
#foreach (var link in Model)
{
#Html.RouteLink(
link,
new { controller = "Product", action = "List", category = link, page = 1 },
new
{
#class = link == ViewBag.SelectedCategory ? "selected" : null,
id= link.Replace(" ","")
}
)
}
I am able to add a class and a id but when i try to add data-filter it returns and error
#class = link == ViewBag.SelectedCategory ? "selected" : null,
data-filter = link.Replace(" ","")
Invalid annonymous type
You cannot use dashes in property names. The solution is to use underscores instead: data_filter = .... ASP.NET MVC automatically converts the underscore to a dash afterwards.
#class = link == ViewBag.SelectedCategory ? "selected" : null,
data_filter = link.Replace(" ","")
As D.R. has pointed out, MVC has the option of turning underscores in dashes.
I am using s#arp architecture 2.0 with asp.net mvc 3.0. The razor code in a master page looks like this:
#Html.ActionLink("Logout", "LogOff", "Users", new { style = "color:Blue;" })
For some reason the app does not produce the correct link anymore (to the action logoff of controller users) but rather points to the current controller for the action logoff. I have not changed anything. where do I have to dig to overcome this please?
The generated link looks like this:
CurrentControllerName/LogOff?Length=5
You are calling a wrong overload of the ActionLink helper. Here's what you do:
#Html.ActionLink(
"Logout", // linkText
"LogOff", // actionName
"Users", // routeValues
new { style = "color:Blue;" } // htmlAttributes
)
It's pretty obvious why this doesn't produce the correct url. You are passing "Users" which is a string value at the place where the helper expects routeValues which must represent an anonymous object.
The correct overload is:
#Html.ActionLink(
"Logout", // linkText
"LogOff", // actionName
"Users", // controllerName
null, // routeValues
new { style = "color:Blue;" } // htmlAttributes
)
I have a code block in my MVC view as follows:
<%using (Ajax.BeginForm("MyAction", new { action = "MyAction", controller = "Home", id = ViewData["selected"].ToString() }, new AjaxOptions { UpdateTargetId = "Div1" }))
{ %>
<%=Html.DropDownList("ddl", ViewData["MyList"] as SelectList, new { onchange = "this.form.submit()" })%>
<%} %>
I want to set the value of ViewData["selected"] so that i can send it to the desired action.
Can anyone please suggest how can i do this?
thanks!
Instead of using a form, why not use a jQuery onChange event on your drop down?
$(document).ready(function() {
$("#ddl").change(function() {
var strSelected = "";
$("#ddl option:selected").each(function() {
strSelected += $(this)[0].value;
});
var url = "/Home/MyAction/" + strSelected;
$.post(url, function(data) {
// do something if necessary
});
});
});
ViewData is not the place to pass data back to the server side. Values of html input controls within form tag are conveniently available in action method. You can get these values either from various types of action method arguments (model, formcollection etc).
Here is a link to free asp.net mvc ebook tutorial. Is a good resource for asp.net mvc.
Found solution at this post it is just small chnge
Yes, that’s right – only change is replacing:
onchange = “this.form.submit();”
with:
onchange = “$(this.form).submit();”