I am facing an issue with dropdownlistfor controller for MVC3 razor engine, that it is not rendering with the selected value.
#Html.DropDownListFor(model => model.EmploymentStatusID, Model.EmploymentStatusList, string.Empty)
Using model am passing the data and everythng is fine till the data get to dropdownlistfor. I attached the image of cshtm page here which I got while debugging. But when rendering the selected option is not coming. What is going wrong here. Thanks in advance.
DropDownListFor does not use the Selected property of the SelectList. It uses the value of property (EmploymentStatusID in your case). In other words, it uses the value from the model, not from the SelectList.
I'm not sure why you're using a MultiSelectList anyways, a DropDownList can only have a single selected value.
Related
This is my Razor view in ASP.NET MVC3 Application
#Html.DropDownListFor(m => m.Leave_Type, Model.Leave_Types, new { #class = "input-append" })
On POST when I read Model.LeaveType, it returns selected value. I want to get the selected text instead, how do I get it? I appreciate any help. Thanks a lot in advance!!
The answer is.. it depends. A drop down is just a standard HTML dropdown control, and it can only post the selected value. It will never post the text, and there is no way to change that.
Do you need the selected value as well? If so, then you can't get both like that. You would have to look up the Leave_Type text in the Leave_Types object based on the selected value returned.
If you don't need the Leave_Type, then you can simply use the text for both the value and text.
Another option might be to write some javascript that copies the selected item text to a hidden field whenever the value of the dropdown changes. Then that hidden field will be posted and you can check that.
I would, however, simply look up the value in whatever method you use to create the Leave_Types collection in the first place.
We have a dropdown list column in ASP.NET MVC 3.0 WebGrid. User will select a item from that list for each row.
When i click on submit I need to capture the selected value for each row.
Can anyone please give me a sample code for doing this from controller.
Thanks in advance
You can make a column with DropDownList control using a html helper:
grid.Column("List", header: "List", format: #<text>#Html.DropDownList("selection", someItems)</text>)
Then put the whole grid in a "form" statement, which refers to "Save" action in your controller, and create that "Save" action method like this:
public ActionResult Save(string[] selection) {
[...]
}
Why don'y you give telerik.mvc grid a try. It's super smooth and handles such scenarios pretty well. Moreover the effort is pretty less in setting it up.
You can download it as a nuget package.
Examples are here http://demos.telerik.com/aspnet-mvc/grid
I'm trying to use a html.dropDownList helper with a strongly typed view model with ajax. I can't the post the code because of the nature of the project.
Here basically what I'm doing...
loading a mvc view via a strongly type view model
clicking a button which does an ajax post to a controller method
using the TryUpdateModel to parse the view model
processing the request
rendering a parital view for the ajax request
According to the article listed below, the problem is that "ASP.NET MVC assumes that if you’re rendering a View in response to an HTTP POST, and you’re using the Html Helpers, then you are most likely to be 'redisplaying a form' that has failed validation."
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx
Instead of "redisplaying the same form value", I need the html.dropDownList to be set to the same value in the view model.
Does anyone know of any custom dropDownList helpers or have any ideas of how to achieve this?
Things I've already tried/considered
per the blog, manually removing the modelstate item...didn't work - didn't pick up the value in the view model - just defaulted to the first item in dropdown list
considered just writing a regular select list...but this is sloppy and cumbersome since I'm rending multiple select lists in a loop
writing my own custom dropDownList helper...wanted to avoid reinventing the wheel
Thanks in advance
Its not fully clear to me what your problem exactly is, but I've had a similar problem. I used the Html.DropDownListFor(, SelectListItem[]) helper. At postback it sets the value to the choosen one. Your postback view doesn't require to have all the fields of the original model.
#Html.DropDownListFor(model => model.SelectedValue, MyModels.DropDownSelectables());
Here I want the selected value as the model.SelectedValue variable and within my (seperate) model I made a array of selectlistitems. The rest is automagic.
Hope it helps, D
I am using MVC3 in ASP.NET. In the Post Action,
I have an View, In which it should let me edit the values. Some fields are read-only and some fields are editable. If there is a validation failure then it should retain the values and display the same View, if there are no error then it should let me to submit the form.
Control1:
Name I have to display as label as It has to be read-only
#Html.DisplayFor(Model => Model.Absentee.Name)
Control2:
I have to display this one as textbox so that it can be edited. This is a required Field.
#Html.TextBoxFor(Model => Model.AbsStart)
#Html.ValidationMessageFor(Model => Model.AbsStart) (This Makes it as a required Field)
Control 3:
This is also a textbox but not required field.
#Html.DisplayFor(Model => Model.AbsEnteredAt)
When there are no validation errors, things works great, but when there is a validation error, then I will be in the same view(Which is what I want) but the value in First control is not retained.
So Basically, when the validation failure, the value is not retained in the DisplayFor Control
What do I do to sort this?
Thank you
As 99% of my answers in the asp.net-mvc tag this one is the same: USE VIEW MODELS. Don't have your controller actions take/pass domain models to/from views. Design view models for each view. View models are classes which are specifically tailored to meet the requirements of this given view. So let's suppose that you have two different views which allow you to edit/update the same domain model. Then you will design two different view models and each view model will contain only the properties that are used by the respective view along with their specific validation requirements.
As far as the DisplatFor helper is concerned, it generates a simple label. Labels are never sent to the server when you submit a form. So you can hope to populate/validate on your controller action only the properties that are contained on the form. For properties that should not be displayed you could use either hidden fields or a single hidden field containing only some unique identifier so that you can fetch from your datastore back the original values (as the user is not supposed to modify them anyways).
As far as the mapping between the view model that you should design and the original domain model is concerned, this mapping is usually performed inside the controller and to simplify it I would recommend you to use AutoMapper.
When there is an error, what is the viewModel you are returning? DisplayFor does not get sent back to the server ... add this under the displayfor:
#Html.HiddenFor(Model => Model.Absentee.Name)
I'm writing the partial view for the child collection ('Vehicles') of my model that renders each element in a table row. My model is therefore in the form of
#model IEnumerable<OrderVehicleViewModel>
For modelbinding to work I need the name of each element to be in the form
Vehicles[0].LicenceExpiry
If I use EditorFor then names are generated in the form
item.LicenceExpiry
If I set the HtmlAttributes with a name value it is seemingly ignored, to get around this I have to do
#Html.TextBox("Vehicles[" + i + "].LicenceExpiry", item.LicenceExpiry)
The problem is then, I lose all the formatting as TextBox doesn't adhere to the DisplayFormat attribute on my model and it means I have to manually build SelectLists for simple boolen properties whereas the default template used by EditorFor does it for me. What is the best way to achieve this?