Dropdownlist selected item not getting displayed in Asp.net MVC - 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.

Related

load a menu partial view on my layout

i need/want to load a partial view that contains my menu, the menu is generated according to the user Roles in menuController, the result is a string that contains a <ul> <li> menu type.
i know that using html.raw() and pass the string using the viewbag it works. but how can i insert the menu on the layout? should i use a partial view? and how?
i also know that i can use ajax but i will like a sync approach
im using mvc4 and razor.
murphy's law at his finest, search for 1 hour, ask on stackoverflow, next link = answer lol
i use #html.action

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

getting dropdown's text in Post action method

I have a Razor view in my asp.net MVC3 application with a dropdownlist like this:
#Html.DropDownListFor(model => model.Account.AccountType, new SelectList(Model.AccountTypes, "AccountTypeCode", "Abbreviation"))
This dropdown is inside a form. When form is posted to action method and viewmodel is filled because of model binding, It get the value(AccountTypeCode) and not the text "Abbreviation" property of dropdownlist. I want to get both of these. how can I get these in post action method.
Please suggest.
If you need more than one property of an object as a value for a dropdown, the easiest way is to create a combination of these in a partial class
EF 4, how to add partial classes this quesion should help you. You will be able to combine values under one property that you will provide to your dropdown helper.
If you don't want to use partial classes I would advise creating your own helper, that will be a lot easier than trying to use something that does not fit your needs. You can do something like :
#helper CustomDropdown(string name, IEnumerable<AccountTypes> valueList)
{
<select>
#foreach (var item in valueList)
{
<option value="#item.Abbreviation #AccountTypeCode">#item.Abbreviation</option>
}
</select>
}
Google "Creating an Inline HTML Helper" to get some valuable resources on that topic
I struggled with this recently, and the best I could do was:
ViewBag.Message = myModel.myProperty.ToString().
in the controller action. Assuming myProperty is AccountType.
Then in my view I just did
#ViewBag.Message
The next problem I encountered is that it spit out the exact text, without spacing. I had to use a helper to add spacing (but it was based on each word being capitalized, so "ThisIsSomeText" would show up as "This Is Some Text".

MVC3 ASPX ViewBag / Model

I have a view that is using a model
Inherits="System.Web.Mvc.ViewPage<Program.Models.Test>"
I am displaying data in the view using:
> <%= this.Model.Item == null ? "" :
> Html.GetString(this.Model.Item.Name) %>
Now I am trying to display a different item from another model on the same view, I thought ViewBag might help, so in my controler, I added:
ViewBag.GuideLines = ctx.GuideLines;
My question is can I display a specific item value from Guidelines model on the same view?
Thanks in advance.
I thought ViewBag might help
Wrong thought. ViewBag/ViewData never helps. I never use those 2 in any ASP.NET MVC application.
Now I am trying to display a different item from another model on the same view
Simply add this other model as property of the main view model. So you will have Model.Guidelines in the view.
in general if you need to use one or more of your domain models for use in a single page, you would create a viewmodel, basically a POCO that would contain just the properties from your domain objects (or any other information) that your view would need to be aware of. you would then strongly ype your view to this new viewmodel type and access the properties as per normal.

Get partial value control value in controller action

I m working on asp.net mvc application.
i have one partial view in that one submit form and click on submit button than data will stored in database.
but when i get data from form collection than that form collection come null so how can i get partial view control's value in action method?
thanks in advance..
note : partial view is not strongly type.
I think you encounter the same issue that is discussed here:
ASP.NET MVC partial views: input name prefixes
To quickly investigate the issue, look at the source code of your page: do your controls from partials have the names you expect? If not, the article above will be helpful.

Resources