Change in MVC ListBoxFor behaviour between projects - asp.net-mvc

Im wondering if anyone can explain the following disparity in functionality between two ASP.Net MVC 3 projects.
In both projects I have a view model which contains the following:
public List<int> Questions;
And in both projects I have the following ListBoxFor code:
#Html.ListBoxFor(x => x.Questions, new MultiSelectList(ViewBag.Questions as List<MyStandardLib.Mvc.Attribute>, "Id", "Name", #Model.Questions), new { #class = "ui-field-multiselect", style = "width: 250px;" })
The difference in functionality is that when run, one project binds the existing selected Questions correctly, the other does not and shows all Questions as unselected. Stepping through the code, the List is populated in the view and is correctly passed to the MultiSelectList constructor, but it isn't setting the values as selected.
This is really frustrating.

Change your ViewBag.Questions variable name to ViewBag.AvailableQuestions. Sometimes the renderer gets confused about what you are referring to.

Related

How to show the value of model in Kendo Custom Editor Template?

I'm using a scheduler of Kendo UI for MVC. I created a Custom Editor Template. It works fine. When click the schedule, it pops a windows and show the info.
I added some properties in the model, the value passed to the editor template. Now, the question is:
How can I show the added properties (just as value, and I do not want to change it) in the editor template popup?
I found that if I use a textbox:
#(Html.TextBoxFor(model => model.Role, new { #class = "k-textbox" }))
It shows a textbox and shows the correct value of Role. However, if I use
<div>#(Model.Role)</div>
It shows blank, just as the value is NULL.
I actually want to show several added properties as a sentence, i.e. I added Role and UserName and I ant to show something like:
UserNameValue has Role of RoleValue
Anyone knows how to do it?
I use this as sample in the code:
http://www.telerik.com/support/code-library/custom-editor
Thanks
Your solution is to put this line because the editor template is binded by mvvm
<div data-bind="text: Role"></div>
Docs
In the code: model => model.Role model is just an alias, not an actual object. You could replace it with m => m.Role and it would function the same.
When you write #Model in your view you are using the object of ContractViewModel which is passed from Controller action, if it is not passed from View it can be null and accessing any property of Model can throw Null Reference Exception and writing Model.Contractors you basically mean ContractViewModel.Contractors
See this post: mvc uppercase Model vs lowercase model

MVC 5 Entity Framework 6 project with Database first, ViewBag, dropdown for foreign key and databinding

I am experimenting with the Entity Framework in a new mvc project, so I created a database, and started out with a database first approach. One of the tables I created had a foreign key to another table, and when the model got created, a virtual property was created to address the key value.
Then I had Visual studio create the controller / views with all the crud. Everything is working fine, but I want to change the dropdowns to Kendo.
The Controller is using ViewBag properties to send the foreign key data back to the view like so:
ViewBag.CourtId = new SelectList(db.Courts, "Id", "Name", tournament.CourtId);
the dropdown looks like this:
#Html.DropDownList("ProviderId", null, new {#class = "form-control"})<br />
I can't figure out how the viewBag data is being bound to the dropdown, nor have I been able to figure out how to substitiute a kendo dropdownlist?
How is this ViewBag data being bound to the dropdown?
so, I found the answer here: Click this Link
Just sub out:
#Html.DropDownList("ProviderId", "Select a Value")
with
#(Html.Kendo().DropDownListFor(model => model.ProviderId)
.OptionLabel("Select a Value")
.BindTo(ViewData["ProviderId"] as SelectList))

Cascading dropdown lists in ASP.NET MVC 5

I am wondering if there's some new helper or method introduced in ASP.NET MVC 5 to implement cascading dropdown lists. I know a way to implement cascading dropdownlist behavior in MVC 3 and MVC 4 that is by using a JSON call
http://www.dotnet-tricks.com/Tutorial/mvc/HL53191212-Custom-Validation-for-Cascading-Dropdownlist-in-MVC-Razor.html
So anyone knows a better way to implement cascading dropdownlists in MVC 5?
I know that this is an old question but somebody still may find it useful
I was searching for the same thing but din't find anything stable and useful so I ended up implementing it by myself:
Please take a look at Mvc.CascadeDropDown helper that I created.
It works with all MVC versions starting from MVC3 and doesn't require any client side libraries(It uses plain vanilla JavaScript).
The usage is very simple:
#using Mvc.CascadeDropDown
//First simple dropdown
#Html.DropDownListFor(m=>m.SelectedCountry, Model.Countries,
"Please select a Country", new {#class="form-control"})
//Dropdown list for SelectedCity property that depends on selection of SelectedCountry property
#Html.CascadingDropDownListFor(
expression: m => m.SelectedCity,
triggeredByProperty: m => m.SelectedCountry, //Parent property that trigers dropdown data loading
url: Url.Action("GetCities", "Home"), //Url of action that returns dropdown data
actionParam: "country", //Parameter name for the selected parent value that url action receives
optionLabel: "Please select a City", // Option label
disabledWhenParrentNotSelected: true, //If true, disables dropdown until parrent dropdown selected
htmlAttributes: new { #class = "form-control" }) //Html attributes
Hopefully it will be helpful for some of you
No, there are no new helpers or methods in MVC 5 to help.
The Ajax HTML helpers have been largely ignored in the update. There are some things that may help with stuff related to this:
There is a new #Html.EnumDropDownListFor() HTML helper to populate a dropdown from an Enum.
The Attribute routing functionality of the has been improved and now works with the Web API so it is much easier to map URLs to API calls.
You can now pass in html attibutes in the EditorFor Html helper #Html.EditorFor(m => m.FieldName, new { htmlAttributes = new { #class = "form-control" } })
I implemented cascading dropdowns last week and used the tried and true JSON call you mentioned. I like to use this jQuery plugin in conjunction with the Web API v2 with the new attribute routing.

Dropdownlist selected item not getting displayed in Asp.net MVC

I have implemented dropdown list in asp.net mvc using following code
In controller
int iSelectedNode=2;
ViewData["ddlModels"] = new SelectList(Models, "ModelCode", "ModelName", iSelectedNode);
In View
<%= Html.DropDownList("ModelCode", (SelectList)ViewData["ddlModels"],"--Select--", new {id="ddlModel" })%>
Still all the time i get to see text "--Select--" selected all the time.
Thanks in advance.
Make sure that your Models collection in the controller contains an element with ModelCode = 2.
This being said as you've tagged your question with asp.net-mvc-2 checkout this answer for a better way to handle drop down lists using strongly typed views and helpers.

ASP.Net Html.DropDownList Selected Element not Selected

I have a site that was using ASP.Net MVC Beta 5, and I have just upgraded it to ASP.Net MVC 1.0. I am having trouble with the selected item in a drop down list.
The follow person has a similar question (Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item) but I there is no answer (other than it might be a bug)
My Controller method looks as follows:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View(Guid id)
{
IntegrationLogic logic = new IntegrationLogic(new IntegrationLinq());
CompanyLogic companyLogic = new CompanyLogic(new CompanyLinq());
IntegrationContainer container = new IntegrationContainer();
container.Sources = logic.GetImportSource(id);
container.Companies = companyLogic.GetCompanies(); // Returns a IList<company>
container.SourceActions = logic.GetAllSourceActions(); // Returns an IList<SourceAction>
container.SinkActions = logic.GetAllSinkActions();
container.SuccessActions = logic.GetAllSuccessActions();
container.FailureActions = logic.GetAllFailureActions();
container.Actions = logic.GetAllActions();
container.Watchers = logic.GetAllWatcherActions();
container.ChainActions = logic.GetAllChainActions();
return View("View", container);
}
The view is a strongly typed against the Model as follows
public partial class View : ViewPage<IntegrationContainer> {}
The problem area in the view template is :
<label for="Companies">Company: </label><%=Html.DropDownList("Companies",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
I am creating a Dropdown List, the selected item never actually gets selected - and that is the problem. "item.CompanyID" is a Guid, "id" is a Guid and "name" is a string on the company object supplied in the IList that is held in the ViewData.Model.Companies instance.
Is this actually a bug ?- I find it hard to understand why this is still present in ASP.Net MVC... I would be totally happy if it is something I have done.
Regardless, what would be the suggested work around?
Thanks
It turns out that if the name of your control via Html.DropDownList is the same name as the collection object it causes an issue with ASP.Net MVC.
So if I change the following code:
<label for="Companies">Company: </label><%=Html.DropDownList("Companies",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
to:
<label for="Companies">Company: </label><%=Html.DropDownList("company",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
all now works. This is because the name of collection on the model was Model.Companies.... bonkers... also note, that changing the case of the name of the control from "Companies" to "companies" does not work either (which makes sense I suppose).
I could change the Model, but as most of it is built using Linq-to-SQL I think it is easier to change the names of the Html elements.

Resources