I've done this a hundred times but not sure what is going on here. I have a DropDownListFor that I populate in the controller like so
var mktsegments = from p in db.ChannelMarketSegment
where (p.ChannelCode != "0")
select p;
ViewBag.Pendist = new SelectList(mktsegments, "Pendist", "Pendist");
And in the view, I am attempting to set the default value of this drop down list with the Pendist value.
EDIT: Pendist is a field that exists in each item pulled into mktsegments via the Linq query.
<div class="M-editor-label">
#Html.LabelFor(model => model.Pendist)<span class="req">*</span>
</div>
<div class="M-editor-field">
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, new { onchange = "ChangeChannel()" })
</div>
However, all this does is set the first value in the list as the default value. If I try to add model => model.Pendist or Model.Pendist as the third paramter in the DropDownListFor like this
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, model => model.Pendist, new { onchange = "ChangeChannel()" })
I either get the following errors
(for model => model.Pendist)
Cannot convert lambda expression to type 'string' because it is not a delegate type
(for Model.Pendist)
'Model' confilcts with the declaration 'System.Web.Mcv.WebViewPage<TModel>.Model'
You are conflicting with the MVC ModelState. When creating Drow down lists, make sure that your property that holds the selected value is not named the same thing as your list of objects. Also, do not use a lambda for the default value, but rather just use the model item directly.
#Html.DropDownListFor(model => model.Pendist, (SelectList)ViewBag.PendistList,
Model.Pendist, new { onchange = "ChangeChannel()" })
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" })
I have 2 fields in a MVC view.
One is #Html.TextBox("txtFirstName")
Second is hidden - #Html.TextBoxFor(model => model.FirstName, new { #type = "hidden" })
The hiddenField is there for posting purposes (as the firstTextbox can be disabled as times).
My question is How do I retrieve the client ID of first Textbox in Javascript ?? I can easily access the client ID of second textbox by '#Html.FieldIdFor(m => m.FirstName)'
The client ID has the modelName prefixed and I dont want to do any hardcoding in Javascript code.
In this case it will be txtFirstName.
But you could explicitly set it:
#Html.TextBox("txtFirstName", "some sample value", new { id = "txtFirstName" })
and then access it as usual:
var value = document.getElementById('txtFirstName').value;
or if you are using jQuery:
var value = $('#txtFirstName').val();
Use HTML helper inside your Javascript:
Javascript & Razor:
var textboxId = '#Html.IdFor(m => m.FirstName)';
There are several extensions:
#Html.IdFor(m => m.FirstName)
#Html.NameFor(m => m.FirstName)
Here is the full list of HTML helper methods http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper_methods(v=vs.108).aspx
Good luck
I have a dropdownlist where a user can select a care provider. In the database the value for this is null however in the application, a value is selected. What do I need to do to have the null value appear as a blank? I thought this was the default behavior. I changed to using strongly typed lists in my view model instead of the viewbag and this may have broken at that time.
Here is the view markup:
<div class="editor-label">
#Html.LabelFor(model => model.PsychologistId, "Psychologist")
</div>
<div class="editor-field">
#Html.DropDownListFor(x => x.PsychologistId, Model.ListPsychologists)
#Html.ValidationMessageFor(model => model.PsychologistId)
</div>
Here is the property from the view model:
[DisplayName("Psychologist")]
public Nullable<int> PsychologistId { get; set; }
Here is the relevant part of my controller:
model.ListPsychologists = new SelectList(XXX, "Id", "DisplayName");
return this.View(model);
where XXX is just the LINQ expression with filtering and sorting criteria. It's been omitted for clarity and space.
The model passed from the controller to the view has PsychologistId being null. And the SelectedValue property on model.ListPsychologists is null.
if PsychologistId is an int, it will assign 0 value to it since int is not a nullable type.
Show your model and controller if my assumption above is not true.
Does your SelectList contain entry with Id==null? I doubt that. And that means that you will have no select list entry matching your value, so browser will select first one available.
Solution: explicitly add entry with Id = null to your SelectList.
I've had these issues before, and what I did to fix it was send the base collection of what I want to fill the dropdown with in the viewmodel, instead of a SelectList. So:
model.ListPsychologists = XXX;
Then in the view:
#Html.DropDownListFor(x => x.PsychologistId, new SelectList(Model.ListPsychologists, "Id", "DisplayName", Model.PsychologistId))
Please verify the SelectList constructor overload I used in MSDN, I'm typing this from memory. Basically you give it the collection of items to use in the SelectList, then the name of the text and value properties (just like you did in your action method code), and the fourth parameter is the value to set the drop down to.
Edit: Also, there is an overload for the DropDownListFor to add a default item in the menu. Like, "Select One".
I have a table(Collection) of Kinds And in Edit View (in MVC) Need this Kinds in DropDown, Also I Need The DropDown Select Old Kind as Default, But there is some Problems.
This is my Select List:
var Kinds = db.Kinds
.Where(n => n.Member.Id == owner.Id)
.Select(k =>k.Title);
var selectList = new SelectList(Kinds);
Also The Default Kind(Value) is :
var Selection= db.Kinds
.Single(k => k.Numbers
.Any(no => no.Id == Model.Id)).Title;
And this is My DropDown:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList)
When I Define DropDown Like following the new member added at the first of dropdown items:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList, Selection)
For ex if the items be [Mobile, Home, Work]
And Selection is [Home]
With above Code the items will be like this: [Home, Mobile, Home, Work]
but I don't need new item, I just want Select Home From The Main Items
How Can I do this?
Thank you
You're currently providing a "default" value for your drop down list. The intended purpose of the dropdownlistfor constructor you are using would be to add an item like "Select Phone" to your drop down list.
Use simply:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList)
Similar question here for reference: DropDownList setting selected item in asp.net MVC