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"})
I keep getting a Compilation Error and can't find matching overloaded method. I've tried a couple ways (variable, variable.toString). Below is the latest try.
When I click on the day (ex: 2) on the calendar the ActionLink should send the querystring: "Index?day=2".
#{ string dayAsString = startCount.ToString();}
<div><span>#Html.ActionLink(#startCount.ToString, "Index?day=" + dayAsString , "Event")</span></div>
Do this
<div>
<span>
#Html.ActionLink(startCount.ToString(), "Index", new { day = startCount })
</span>
</div>
The last parameter creates an anonymous object with the property day and value startCount. ActionLink knows to convert that into a querystring using the property name and the property value.
More details here http://msdn.microsoft.com/en-us/library/dd492936.aspx
Edit:
If you want to target a specific controller, do this
#Html.ActionLink(startCount.ToString(), "Index", new { controller = "Event", day = startCount })
You can also do this
#Html.ActionLink(startCount.ToString(), "Index", "Event", new { day = startCount }, null)
but I don't like passing null as a parameter.
Here's a list of all the overloads: http://msdn.microsoft.com/en-us/library/dd505040.aspx
You can also just cycle in the intellisense.
This should work
#Html.ActionLink(#startCount.ToString,"Index","Yourcontroller",new { day=#startCount.ToString()} , null)
replace Yourcontroller with your controller name
I'm building my first MVC application after years of doing webforms, and for some reason I am not able to make this work:
#Html.DropDownList("PriorityID", String.Empty, new {#class="textbox"} )
Error message:
System.Web.Mvc.HtmlHelper<SPDR.Models.Bug>' does not contain a definition for DropDownList and the best extension method overload System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object) has some invalid arguments
Any help greatly appreciated!
Looking at the controller, and learing a bit more about how MVC actually works, I was able to make sense of this.
My view was one of the auto-generated ones, and contained this line of code:
#Html.DropDownList("PriorityID", string.Empty)
To add html attributes, I needed to do something like this:
#Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { #class="dropdown" })
Thanks again to #Laurent for your help, I realise the question wasn't as clear as it could have been...
UPDATE:
A better way of doing this would be to use DropDownListFor where possible, that way you don't rely on a magic string for the name attribute
#Html.DropDownListFor(x => x.PriorityID, (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { #class = "dropdown" })
As the signature from the error message implies, the second argument must be an IEnumerable, more specifically, an IEnumerable of SelectListItem. It is the list of choices. You can use the SelectList type, which is a IEnumerable of SelectListItem.
For a list with no choices:
#Html.DropDownList("PriorityID", new List<SelectListItem>(), new {#class="textbox"} )
For a list with a few choices:
#Html.DropDownList(
"PriorityID",
new List<SelectListItem>
{
new SelectListItem { Text = "High", Value = 1 },
new SelectListItem { Text = "Low", Value = 0 },
},
new {#class="textbox"})
Maybe this tutorial can be of help: How to create a DropDownList with ASP.NET MVC
If you are add more than argument ya dropdownlist in Asp.Net MVC.
When you Edit record or pass value in view bag.
Use this it will be work:-
#Html.DropDownList("CurrencyID",null,String.Empty, new { #class = "form-control-mandatory" })
There are some options in constructors look,
if you don't have dropdownList and you wanna insert CSS class you can use like
#Html.DropDownList("Country", null, "Choose-Category", new {#class="form-control"})
in this case Country is the name of your dropdown, null is for you aren't passing any generic list from your controller "Choose-Category" is selected item and last one in CSS class
if you don't wanna select any default option so simple replace "Choose-Category" with ""
You Can do it using jQuery
$("select").addClass("form-control")
here, Select is- html tag,
Form-control is- class name
#Html.DropDownList("SupplierId", "Select Supplier")
and here, SupplierId is ViewBagList,
Select Supplier is - Display Name
Simply Try this
#Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { #class="dropdown" })
But if you want a default value or no option value then you must have to try this one, because String.Empty will select that no value for you which will work as a -select- as default option
#Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, String.Empty, new { #class="dropdown" })
Try below code:
#Html.DropDownList("ProductTypeID",null,"",new { #class = "form-control"})
Try this:
#Html.DropDownList(
"country",
new[] {
new SelectListItem() { Value = "IN", Text = "India" },
new SelectListItem() { Value = "US", Text = "United States" }
},
"Country",
new { #class = "form-control",#selected = Model.Country}
)
You can simply do this:
#Html.DropDownList("PriorityID", null, new { #class="form-control"})
Try This
#Html.DropDownList("Id", null, new { #class = "ct-js-select ct-select-lg" })
I'm having a problem trying to add a custom HTML5 data attribute to the table that is rendered using the WebGrid helper. I want the table tag look as follows:
<table data-test="testdata"><!-- Table Content --></table>
Here is a sample view using the Razor view engine:
#{
var myUser = new
{
Id = 1,
Name = "Test User"
};
var users = new[] { myUser };
var grid = new WebGrid(users);
}
#grid.GetHtml(htmlAttributes: new { data-test = "testdata"})
The last line will produce a "Invalid anonymous type member declarator." error, because of the hyphen in data-test.
With some of the other input HtmlHelpers, you can use an underscore in place of the hyphen and it will be automatically changed to a hyphen when rendered. This does not happen with the WebGrid.
If I pass in a dictionary for htmlAttributes:
#grid.GetHtml(htmlAttributes: new Dictionary<string, object> {{ "data-test", "testdata"}})
the table gets rendered as such:
<table Comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" Count="1" Keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" Values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]"><!-- Table Content --></table>
What am I doing wrong and what should I do render the attribute as desired?
I am afraid that this is not possible. Unfortunately the WebGrid it doesn't support the same syntax as standard HTML helper such as TextBoxFor where you could:
#Html.TextBoxFor(x => x.SomeProp, new { data_test = "testdata" })
and the underscore would be automatically converted to dash.
I want to add two default options to my dropdowns in asp.net MVC using the html helper class, they are "--Please Select--" and "--Other--".
I can add one static option using
<%= Html.DropDownList("ddlserviceGroup",
(IEnumerable<SelectListItem>)ViewData["ServiceGroups"], "--Select Item--")%>
But i need to add two options and can't seem to figure it using the HTML helper class
The existing helper doesn't allow this. I'd suggest adding the options in the controller or perhaps writing your own extension method.
var serviceGroups = db.Groups
.Select( g => new SelectListItem
{
Text = g.Name,
Value = g.ID
})
.ToList();
// prepend to list
serviceGroups.Insert( 0, new SelectListItem
{
Text = "--Select Item --",
Value = string.Empty
} );
// add at end
serviceGroups.Add( new SelectListItem
{
Text = "-- Other -- ",
Value = ????
});
ViewData["ServiceGroups"] = serviceGroups;
In View
<%=
Html.DropDownList("ddlserviceGroup",
(IEnumerable<SelectListItem>)ViewData["ServiceGroups"]
%>