#Html.DropDownListFor(model => model.TransferAvailibilities.First().CancellationID,
(ViewBag.CancellationSchema == null ? null :
(IEnumerable<SelectListItem>)ViewBag.CancellationSchema), new { #class = "field large" })
And error:
There is no ViewData item of type 'IEnumerable' that has the key 'CancellationID'.
I know that but i will bind data with ajax when i need. And my lambda not work or something for DropDownListFor...
Create SelectList from your ViewBag.CancellationSchema like this:
#Html.DropDownListFor(x => x.intClient, new SelectList(Model.Clients, "ClientId", "ClientName"), string.Empty);
You doing it wrong. Instead of explaining how and why, please take a look into this posting, will clarify things for you on how to use #Html.DropDownListFor:
How to write a simple Html.DropDownListFor()?
Related
I have a problem when I want to bind a value from my model to a textbox, here is my MVC view code:
#Html.TextBox("SellerBroker", model => model.OutOfMarket.BuyerBroker.Name , new { #class = "control-label" })
I want my textbox to have a name or 'SellerBroker' and it's value to come from my model property model => model.OutOfMarket.BuyerBroker.Name and with HTML attributes of class = "control-label". However, I am receiving the following error:
Cannot convert lambda expression to type 'object' because it is not a delegate type
The #Html.TextBox() can be used for generating a textbox with an initial value (one way binding).
If you want to really bind the textbox to your class property (two ways binding), you should use the #Html.TextBoxFor() helper. This method take as parameter a lambda expression, as used in your example.
You can found more details on TextBox helpers at : Html.Textbox VS Html.TextboxFor
Helper #Html.TextBox() does not contain overloard that have lambda parameter. You should use it without lambda like this like #Stephen Muecke advice you:
#Html.TextBox("SellerBroker", Model.OutOfMarket.BuyerBroker.Name , new { #class = "control-label" })
If you want to use lambda you should use #Html.TextBoxFor() helper. But you should change name like this:
#Html.TextBoxFor(model => model.OutOfMarket.BuyerBroker.Name, new { Name = "SellerBroker", #class = "control-label"})
I have my get and post methods . I populate my data during post methods based on some value. When i try to run the program it gives me a here is no ViewData item of type 'IEnumerable' that has the key because there is no data in the dropdown. How can i show empty dropdown and bind the same during post method .
The DropDownListFor template must be provided with an IEnumerable to work properly. If you want an empty list, the best way to provide an empty IEnumerable is to use the Enumerable.Empty<> static method.
Your code would then look like this:
#Html.DropDownListFor(model => model.Name, Enumerable.Empty<SelectListItem>(), "-- Select Name --", new { #class = "form-control" })
Think I am doing something silly here.
I have :
#Html.DisplayFor(modelItem => item.DateCreated,"ShortDateTime")
I need to display this datetime like "01/01/2013 20.13", and I wish to do it via Display Templates. I have a partial view in Shared/DisplayTemplates called "ShortDateTime.cshtml" with the following code:
#model System.DateTime?
#Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty
The value can be null by the way.
I believe my Template is incorrect, and I need corrections on this please.
Many thanks in advance.
Try:
#Html.DisplayFor(model => model.DateCreated, "ShortDateTime")
and in your template:
#model System.DateTime?
#(Model.HasValue ? Html.Raw(Model.Value.ToString("dd/MM/yyyy")) : null)
I get an error when trying to pass a collection of Enums to a DropDownList.
The collection is of type IEnumerable.
The error states: "Cannot resolve method DropDownListFor( lambda expression, System.Collections.Generic.IEnumerable"
The code:
#Html.DropDownListFor(m => listing.WorkflowStatus, Model.WorkflowStatuses, new { id = listing.WorkflowStatus, onchange = "$(this.form).submit()" })
I'm completely stuck. Can anyone advice me on what the problem might be?
Check out a helper I made to do just this.
http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23
You need to turn them into a select list
In your controller, convert your enum to an IEnumerable and add it to your ViewBag then reference it in your view
Controller:
ViewBag.WorkflowStatuses = EnumHelper.SelectListFor(WorkflowStatus.Option1);
In the view (something like....)
#Html.DropDownListFor(m => listing.WorkflowStatus, ViewBag.WorkflowStatuses as IEnumerable<SelectListItem>.....
I have a little problem. I use the Html.DropDownListFor helper to render a dropdown list to the client.
But I can't set the selected value in dropdown list.
<%= Html.DropDownListFor(model => Model.CalculationClassCollection,
new SelectList(Model.CalculationClassCollection, "ID", "Name", 3 ),
new { id = "ddCalculationClass" })%>
Anybody can help me?
If the SelectList is not a member of my Model, I'd use <%= Html.DropDownList("listBoxID", new SelectList(Model.CalculationClassCollection, "ID", "Name", 3 ))%>
It is one of the overloaded methods. I think it is the method with a parameter something like default. Sorry I am not in front of dev PC right now so I can't tell you exactly which method it is.