Asp.net MVC DropDownList Add and Update - asp.net-mvc

I am quite new to this as my background is ASP.net Web Form. I am faces with 3 issues as followed:
Dropdownlist postback and other dropdownlist is showen once the user selected the 1st dropdownlist.
2nd, I am trying to pass the dropdownlist selected value in adding data
#Html.DropDownList("CategoryID", ViewData["Category"] as List)
I am trying to update the data based on the selectedvalue
#Html.DropDownList("CategoryID", ViewData["Category"] as List)
I am able to populate it from the database.
Any advice would be helpful. Many thanks

The answer to this question is
#Html.DropDownListFor(model=> model.CategoryID, ViewData["Category"] as List<SelectListItem>)
and you use FormMethod.Post

Related

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))

How to Fill ListBox

I think my question is basic but I can't find the way to do this issue in Asp.Net MVC 3.
I have a Model with the Controller called "Concepto". I want to see into the Create view, a ListBox with all the data from Concepto model.
The Index view show the complete data from the model, so the database connection and EF process are correct.
Hi you should create a strongly typed view using the model. Once you have the model in the context of the view, you can use the MVC helpers to render the data. For example if you have multi line postal address data stored in a property PostalAddress, you can use
#Html.TextAreaFor(m => m. PostalAddress)
You can further customise the text area by providing col and row information as the second parameter
#Html.TextAreaFor(m => m. PostalAddress, new {#cols=”50”, #rows=”20”})
Hope this helps
You can't display all the data in a ListBox. However, you can have a field as List itme's display text and another as its value (like ID) using #Html.ListBoxFor() method which expects a name of a field and a collection of item eg. #Html.ListBoxFor(model => m.someId, Model.CollectionData);
Thanks,

How do you render 2 models on a view in asp.net mvc 4?

I have a page that will have some textboxes that will save to a table called Values, let's say. The textboxes that I display will populate the selection from a dropdownlist. This dropdownlist will get its values from another table in the database called DropDownListValues. I am assuming that my controller would be built on the Values model but I guess I am a little bit confused as to how I will give the values to the dropdownlist. Do I store it in a ViewBag or render a partial view? Just trying to find the best approach here as I am a little new to asp.net mvc.
Yes, you store them in viewBag
ViewBag.CompanyTypes = _Repository.GetCompanyTypes().
Select(p => new SelectListItem { Text = p.CompanyType1, Value = p.CompanyTypeID.ToString() });
and use it like that:
#Html.DropDownListFor(model=> model.CompanyTypeID, (IEnumerable<SelectListItem>)ViewBag.CompanyTypes )
I think that you can use both of them and question is store in ViewBag or render partial view? answer: yes, you can use both of them when you need. And you can prepare specific extensions for example for cities, offices, few popular users etc.
Use a ViewModel. This was in a comment but I cannot accept that as an answer so I decided to post an answer

ASP.NET MVC 2 DropDownList Selected item changed?

I have a drop down list on my aspx form and what I want is to refresh the site after I select an item from the list. This is my drop down list:
<%: Html.DropDownListFor(x => x.CompanyUserFilterId, new SelectList(Model.CompanyUsers, "Id", "FirstName", Model.CompanyUserFilterId))%>
I use it to filter the data shown on the form depending on the selected item on the drop down list. Please help :)
you'll need to 'refresh' the site with some sort of client side mechanism onchange. A similar solution using JQuery is posted here.
The simplest method is probably to trigger an html/javascript onchange-event, and handle that to update your page (or post the form with the selected Id).
Take a look at this: http://blog.wekeroad.com/2008/10/21/asp-net-mvc-dropdownlist-and-html-attributes

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.

Resources