Mvc 3 Pre-select Html.DropDownListFor Not Working In Nerd Dinner?
Jan 26, 2011
Learning about dropdown lists, Im trying to add a RSVP create page for nerddinner as in Scott Gu's blog with a Html.DropDownListFor listing available dinners. I can get the dropdown list populated but I cannot get the dropdown to pre select the value ("Sample Dinner 2") I want. Im using an initilizer to seed a few dinner objects in the db. The database is sql ce 4 using a EF 'code first approach'. Sorry I know this is a v common problem and hate to ask, but honestly have spent quite some time on this but cant get it to work:
ViewModel
public class RSVPViewModel
{
public SelectList DinnersList { get; set; }
public RSVP Rsvp { get; set; }
public string SelectedItem { get; set; }
}
[code]...
So is not preselecting the dropdownlist with the value "Sample Dinner 2" when the page loads. The list displays ok and sets the correct DinnerID when I make a selection and click Submit.Tries this also:
but doesnt set or bind to Rsvp.DinnerID.This preselects from the list but doesnt bind (or set Rsvp.DinnerID)
@Html.DropDownList("DinnersList")
I want to keep it mvc3 so want to implement with strong type using ViewModel approach (no ViewData) and preferably using Html.DropDownListFor (not Html.DropDownList).Viewbag seems unnecessary for this case.Thinking I should be using a selectList of selectListItems I tried this verbose approach :
RSVP rsvp = new RSVP();
string selected = "2";
List<SelectListItem> dinners = new List<SelectListItem>();
foreach (Dinner dinner in dbc.Dinners.ToList())
{
SelectListItem slDinner = new SelectListItem();
slDinner.Value = dinner.DinnerID.ToString();
slDinner.Text = dinner.Title;
slDinner.Selected = (slDinner.Value == selected);
dinners.Add(slDinner);
}
var dinnersList = new SelectList(dinners, "Value", "Text", selected);
var viewModel = new RSVPViewModel { DinnersList = dinnersList, Rsvp = rsvp, SelectedItem = selected };
However still no work. Should I be making use of the ViewModel SelectedItem property in: @Html.DropDownListFor.. somehow? Something like :@Html.DropDownListFor(x => x.SelectedItem, Model.DinnersList)but how to I get a selected value to set Rsvp.DinnerID. I think thats called binding.
I'm new in ASP.NET MVC 2 and I'd like to write a very simple dropdown list which gives static options. For example I'd like to provide choices between "Red", "Blue", and "Green".
I am looking at ASP.NET MVC2 and trying to post a complex object using the new EditorFor syntax.I have a FraudDto object that has a FraudCategory child object and I want to set this object from the values that are posted from the form.Posting a simple object is not a problem but I am struggling with how to handle complex objects with child objects.I have the following parent FraudDto object whcih I am binding to on the form:
public class FraudDto { public FraudCategoryDto FraudCategory { get; set; }
The SiteId is of the type System.Guid. When this is posted to the action of my controller, then ModelState.IsValid returns false, which is actually normal because I have this in the generated HTML:
[Code]....
How can I make sure that the first option is <option value="0"></option>?
I'm trying to use the Nerddinner as a model to built my own project. I'm using the repository method but I cannot fiqure out why the Edit does not save to the database. I tested in the Nerddinner and it failed to save as well. Is this not functional in Nerdinner or could it be due to me using VS 2010 express?
I also started using MVC3 so I thought that could be the problem. I did some testing and was able to get my Edit to update to the database if I have the controller access the database instead of the repository.
i m facing a issue in my code i m calling a user control which is in master page, and in row databound i m calling it and it is working fine but on select index change of checkbox it is not working
Use Html element SELECT in aspx page like:Data is feed by setting its property DataSource:MySelection.DataSource = IEnumerable collection DataSuppose data is stored in table in DB like:
1 - Option A 2 - Option B 3 - Option C 4 - Option D
From MySelection.Value, I can get the ID value, like 1,2,3.From MySelection.Name, I will get "MySelection", not the selected item name like "Option A"How to get the selected item Name?
I have a comment form inside the blog posts. in the form, if the user is authenticated, I will assign the values of the user into textboxes. that part is as follows;
[Code]....
But when I add HTML.TextBox instead of HTML.TextBoxFor, I am not getting validation if the user erase the fileds. Also, this form is inside the indext page and it is related to another action as follows;
[Code]....
so I cannot assign ModelState.AddModelError from controller.
Why is it that client validation is getting triggered saying that my Default Theme field is required even if I didn't specify a [Required] attribute in my model?
public class Site { public int SiteId { get; set; } Required(ErrorMessage = "*")] [LocalizedDisplayName("Title")] public string Title { get; set; } [Required(ErrorMessage = "*")] [LocalizedDisplayName("RootDirectory")] [code]...
I have a table, and each row has a column where its data is actually a select list. I want the user to be able to select a drop down item, and when they POST, it saves the Id of the selected item. Is this possible?
I'm trying to model a "Room" each room has a number of Jacks, there are 2 types of Jacks (Data/Voice) - this is the select list.
[Code]....
Something like this does not work. I created a stub in my view model
I use the Entity Framework and Linq to Entities with MVC 2.0. When use the DropDownListFor from Html helpers to populate a dropdown that contains an ID from a foreign object, I post the form data but I canīt set the correct relation. Using the debug, I can see the ID retrieved from dropdown at my returned foreign object ID, but I think that I not use the association correctly.
I am trying to populate a drop down list from my controller for a create action. I can display the dropdown as I want but when I cannot get the value to be stored in the database. My models:
[Code]....
Controller: [Code]....
View: [Code]....
This above snippet will show me the message "The value '50117dae-a7d7-4371-926f-7b1f7aaec133' is invalid."
if I change the line to @Html.DropDownListFor(ct => ct.CollectionType.CollectionTypeId, new SelectList(ViewBag.CollectionTypes as System.Collections.IEnumerable, "CollectionTypeId", "Title")) @Html.ValidationMessageFor(model => model.CollectionType)
the page will not do anything - I believe this could be because the controller is trying to add a new CollectionType.
Lets assume that I have retrieved page html using HttpWebRequest & StreamReader. Now I would like to cut one div from the loaded html and put it in literal on my asp.net page. I know that that div has css class content. How can I do it?
Looks fine to me. But the strange thing is that in the copied solution the browser doesn't render what it should be rendering. The size property seems to be ignored. It just gets rendered as if the size was set to one.
This applies to all listboxes in the solution. I compared the masterpages and web.configs and there are no differences. This issue also applies to all browsers. I used IE8 Developer tools to compare the documentmodes and they are the same for both solutions.
i have been having trouble trying to find out what is wrong with my sql select query... i wanted to select a sessionID by comparing the tourID and dates. the dates datatype at the database is datetime and the value of itemSessionDate is in string. i tried to convert itemSessionDate to date and dateAndTime but it gave me mm/dd/yyyy while the date in my database is dd/mm/yyyy hh:mm:ss. so i thought i can try using the itemSessionDate string as it has the same value as the one return from the database. but it keeps on goin to the if not reader.hasRows and quit the statement
I am using the stored procedure below for the select command of a SqlDataSource, and I'm trying to pass a query string parameter, but I get this error:
Procedure or function 'ActivationCampaignGetById' expects parameter '@campaignId', which was not supplied.
I am using the stored procedure below for the select command of a SqlDataSource, and I'm trying to pass a query string parameter, but I get this error:
Procedure or function 'ActivationCampaignGetById' expects parameter '@campaignId', which was not supplied.
I have a View that has a select drop-down list and an edit button within a form. What I want to do is have the user select one of the options from the select element, click on the edit button, and get the value of the selected option in the Controller method.
I created my form with <% Html.BeginForm(): %>
and the Controller "Edit" method should be called.
I looked through several online examples, but each one focused on how to set values and the default selected value of the select element, rather than retrieving it from the code in the Controller method. I tried grabbing it from the Request.Form collection like:
string val = Request.Form["myDropDownList"].ToString(); and also: string val = Request.Form["myDropDownList"];
Obviously I didn't do this correctly, because I get a null reference exception. Does anyone have the solution to this issue?