C# Mvc 3 Using Selectlist With Selected Value In View

Jan 2, 2011

I'm working on a MVC3 web application. I want a list of categories shown when editing a blo from whe applications managements system. In my viewmodel i've got the following property defined for a list of selectlistitems for categories.

/// <summary>
/// The List of categories
/// </summary>
[Display(Name = "Categorie")]
public IEnumerable<SelectListItem> Categories { get; set; }

The next step, my controller contains the following edit action where the list of selectlistitems is filled from the database.

public ActionResult Edit(Guid id)
{
var blogToEdit = _blogService.First(x => x.Id.Equals(id));
var listOfCategories = _categorieService.GetAll();
var selectList = listOfCategories.Select(x =>new SelectListItem{Text = x.Name, Value = x.Id.ToString(), Selected = x.Id.Equals(blogToEdit.Category.Id)}).ToList();
selectList.Insert(0, new SelectListItem{Text = Messages.SelectAnItem, Value = Messages.SelectAnItem});
var viewModel = new BlogModel
{
BlogId = blogToEdit.Id,
Active = blogToEdit.Actief,
Content = blogToEdit.Text,
Title = blogToEdit.Titel,
Categories = selectList //at this point i see the expected item being selected
//Categories = new IEnumerable<SelectListItem>(listOfCategories, "Id", "Naam", blogToEdit.CategorieId)
};
return View(viewModel);
}

When i set a breakpoint just before the view is being returned, i see that the selectlist is filled as i expected. So at this point everything seems to be okay. The viewmodel is filled entirely correct.
Then in my view (i'm using Razor) i've got the following two rules which are supposed to render the selectlist for me.

@Html.LabelFor(m => m.Categories) @Html.DropDownListFor(model=>model.Categories, Model.Categories, Model.CategoryId)
@Html.ValidationMessageFor(m => m.Categories)

When I run the code and open the view to edit my blog, I can see all the correct data. Also the selectlist is rendered correctly, but the item i want to be selected lost it's selection. How can this be? Until the point the viewmodel is being returned with the view everything is okay. But when i view the webpage in the browser, the selectlist is there only with out the correct selection. What am I missing here? Or doing wrong?

View 2 Replies


Similar Messages:

Getting The Selected Value On A Mvc Selectlist?

Sep 14, 2010

I'm failing at getting the selected item on a mvc dropdown list. I can't stand viewdata, and every example uses it. Here is my code,

//controller
public ActionResult Register(Models.Person APerson)
{
}
public class Person
{
public Person().............

View 1 Replies

MVC :: Why SelectList Don't Have A Default Constructor New SelectList

Aug 20, 2010

why SelectList dont have a default constructor new SelectList()

View 1 Replies

MVC :: Identified / How To Fix SelectList In Helper

Jan 9, 2011

I followed MVC 2 source to create a CheckBoxList helper.

I was able to identify the problem I am having but not able to solve it.

On my view I have the following:

[Code]....

The problem is in the bold part ...

The newSelectList is equal to selectList but all items were deselected.

What am I missing?

View 3 Replies

MVC :: Jquery Disable First Value In A Selectlist?

Jun 13, 2010

how can i disable first value in the select list when the checkbox is clicked

View 2 Replies

MVC :: Selectlist Could Not Be Found Error?

Dec 24, 2010

i have wrote the following code inside a model class, but there was an error that indicates that the select list could not be found:-

public SelectList Countries {
get;
private
set; }

so which "using system" i should add????

View 2 Replies

Change Contents Of SelectList?

Jan 14, 2011

Is there a way to remove items from a SelectList?

View 1 Replies

MVC :: SelectList And Dropdown Value - Get A Null?

Nov 18, 2010

I cannot get my code to work with the usual workarounds. I have a model which exposes a SelectList and a value for the selected item in it. The list is built like: new SelectList(persons, "Id", "Name");

where persons is an array of type Person with a *string* property named Id and a string property named Name. My model has a SelectList property returning a list built as shown, and a SelectedPersonId *string* property which should be updated by the dropdown. Here's my view code:

Html.DropDownListFor(m => m.SelectedPersonId, Model.PersonList)

I can see the persons in the dropdown and select one item, but the model's SelectedPersonId property never gets updated (I get a null). Of the two workarounds I found googling around, one stated to build the SelectList in the model, using the constructor shown above; another recommended to ensure that the selected value is a string. I did both, but still it does not work.

View 5 Replies

MVC :: Instantiating SelectList In Code?

Feb 25, 2011

I had been building my SelectLists in the View, but I found examples doing it in code that I liked better. Now I'm creating one of my SelectLists like this:

[Code]....

And all is good, but I'm not sure how to do two things. First, I'd like to add an initial SelectListItem that prompts the user to select a value. When instantiating the SelectList in a loop this is easy, but I was hoping to keep this approach. The second problem is setting the default selected item. In the constructor it shows the 4th parameter to be used for this purpose, but I'm not certain how to do it. There's a property of this object named "DefaultView". It's a boolean, and I'd like that to determine the selected state. Is this possible?

View 2 Replies

To Get The EditorForModel / Want To Use A SelectList Instead Of A TextBox For Status?

Jul 1, 2010

I have written an Enum extension method that allows me to create a view model and allows me to easily bind a generic Enum to a SelctList like:this.ProductStatusList = new ProductStatusTypes().BindToSelectList<ProductStatusTypes>
(product.Status.ToString());

In my View I can then have:

<% using (Html.BeginForm()) {%>
<fieldset> [code]...

Notice I am using EditorForModel and then a seperate DropDownList. My question is: Is there a way to get the EditorForModel to be clever enough to pick up that I want to use a SelectList instead of a TextBox for status?

View 1 Replies

C# - Set Custom Text Field In SelectList?

Feb 13, 2011

I'm trying to display a dropdown on my view page that has a custom text value.

I'm trying to display a list a Contacts. A Contact contains a ContactID, FirstName, and LastName.

<%= Html.DropDownListFor(m => m.ContactId, new SelectList(Model.Contacts, "ContactID", "LastName"), "- Select a Contact -") %>

Right now I'm just displaying the last name, but I'd like to display the first name and last name in the dropdown.

View 1 Replies

Does Model Binding With A Selectlist Work?

Jan 5, 2011

I'm having problems retrieving the values of a selectlist in my form collection. I've tried making a viewmodel with an attribute with the same name as the select list.

I'm honestly just realizing I REALLY don't understand how model binding works with selectlists. I've just been assuming that the following conventions apply:

Name the select list the same thing as the attribute on the model you want it to bind to. Apart from that, I really don't get it. I've looked at several books on it and they're useless frankly. How does a select list work with a) form collection and b) a particular model?

View 1 Replies

MVC :: Set DataValueField Of SelectList When Binding To A List?

Jan 14, 2010

What do I use to set the dataValueField of the Select List when I'm using a list of strings. I want to be able to set the value of the option in the select. If i have:

List<string> list = new List<string>();
list.Add("apple");
list.Add("orange");

and I want my html to be:

<select>
<option value="apple">apple</option>
<option value="orange">orange</option>
</select>

View 2 Replies

Storing SelectList ID Using MVC EditFor Templates & Annotations (.net 4, Mvc3)

Mar 23, 2011

I can currently populate and display a Drop Down using the following model annotations, viewmodel, and custom Editor template - but am having problems persisting the submission (more on that below):

//Domain
Public class Person
{
public string FirstName {get;set;}
public string LastName {get;set;}
public string ClassName {get;set;}
public int ClassId {get;set;}
...
}
//ViewModel
Public class PersonViewModel
{
public Person Person {get;set;}

Like I mentioned, the above works fine - as in the drop down is displayed properly, but obviously in its current state it's not saving the classID (or name) to the Person entity, I understand the problem but not sure how to fix it.

My questions:

How can I save the selected value from the drop down to the Person entity
How can I save both the ID and the Name into the Person entity - I'm guessing only way is a hidden field that gets populated with an onchange event (this sounds weird, I'm sure, but it's being stored in a document DB, so it's not normalized - I may just store Name in the end)

View 1 Replies

MVC :: How To Set Class Value For Selected Ul Tags In Mvc View

Jul 20, 2010

i have view which display product list..this is how html looks like

<% foreach (var products in Model.Products) { %>
<ul id="<%: products.ProductID %>" class="">
<li>
<a href="">xx</a>
</li>
</ul>
<% } %>

This page will list 16 product items,means 4 by 4.MY question is,i need to set class value for in 1st,5th,9th and 13th ul tag.

smthing like this

<ul class="first"><ul><ul><ul>
<ul class="first"><ul><ul><ul>
<ul class="first"><ul><ul><ul>
<ul class="first"><ul><ul><ul>

how can do this with foreach...

View 2 Replies

C# - Convert List<Roomtype> To SelectList For ASP, MVC DropDownFor<>, And Get The Correct Values Per Option Item?

Jun 1, 2010

I have my class RoomType:

Int32 Id
String Name
String ColorCode

My viewmodel gets a List<Roomtype> RoomTypes which should be displayed in a dropdown.

Each dropdown option item should have: 1) as title the Name, 2) as value the Id, and 3) the style background-color #ColorCode.

My problems are how to convert this list correctly into a List<SelectListItem> as required by ASP.NET MVC's DropDownFor helper, and then to have the correct values inserted for each option.

I've tried to have a new readonly property in my viewmodel, which has a getter RoomtypeSelectList which returns new SelectList(RoomTypeList) but I can't get the correct properties to show (Name, Id, Background color).

View 3 Replies

MVC :: Get Value Of A View's Selected RadioButton From A Controller Action?

Jul 15, 2010

I have a simple controller that sends a model/data to a View:

[Code]....

The View displays the data in a HTML Table:

[Code]....

This View displays the data correctly and provides a RadioButton for each row. When I select one of the RadioButtons the previous selection is removed as expected.

For now I am using an ActionLink to enter another Controller action. I have tried many ways without success to retrieve the selected RadioButton's value in this controller action.

[Code]....

I have also tried defining the Controller action with a parameter matching the model without success.

[Code]....

View 2 Replies

Forms Data Controls :: How To Get The ID Of Selected Row In Grid View

Feb 15, 2011

I have this grid view, binded by code in C#

when I press reports link, I want to make some action according to the ID related to selected row

View 5 Replies

Web Forms :: View Data From A Database Based On The Selected Area From Tag?

Apr 27, 2010

i have this code in html but i dont know why i cant take the correct value

the "xcity" take only the last value ??

[code]....

View 3 Replies

Forms Data Controls ::want To Get Selected Items On Grid View?

May 16, 2010

how can i get selected checkbox item from grid view if the user click on the buttion , i tryied this but i dont know how to get the values from columns.

my columns are Id,FirstName.the function called from the buttion not from the gridview.

my code is:

[Code]....

the secound one, when i am trying to insert a new values to db and the new values get id automaticly by sqlserves 2005,how can i get this id because i want to save in other table to.

View 3 Replies

Forms Data Controls :: How To Get The Selected Row Index In Grid View

Mar 11, 2010

I have a gridview with dynamic buttons for edit and save.Save button is disable by default. In the edit button command argument i bind the id of the record.

now when i click edit button i want my save button to get enable and edit button will disable for that particular row. I dont kow how to get the selected row index through dynamic button.

View 3 Replies

Web Forms :: Displaying Selected Grid View Items In The Treeview?

Jan 18, 2010

I am working with grid view and tree view. My requirement is when the item is selcted in the gridview it should bind it to the particular node of tree view. Can you please help me out. Its very urgent. Can you please send me with the sample code.

View 2 Replies

Forms Data Controls :: Find Selected Row Value From Grid View?

Mar 15, 2011

i want to find selected row value in asp gridview from column name ?

how can i do that ?

View 4 Replies

Forms Data Controls :: Grid View Do Not Give Selected Value?

Jan 25, 2011

I have a gridview and a details link. Clicking the detail link should take me to a details page with lot of formviews in it. But clicking on the Details link gives me error : 'RadButtonList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

View 2 Replies

Data Controls :: How To Keep Current View Selected In MultiView After Redirect

May 7, 2015

I used:

1 Response.Redirect("Products.aspx");

But I have in my Products page many views and the view that I used for delete is view4 how to make Response.Redirect reach to this view of delete...

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved