I have class include the following objects
public class ProductViewer
{
public J_Items Item { get; set; }
public List<J_ItemsImages> lstItemImages { get; set; }
}
how can i add DropDownListFor Item.ItemName
J_Items definition
public int ID { get; set; }
public string ItemName { get; set; }
public string ItemShortDesc { get; set; }
public string ItemLongDesc { get; set; }
#Html.DropdownlistFor(m => m.Item.ItemName)
Where m is your ProductViewer class, of course that should be made in to a viewmodel.
You can use the first argument of the Html.DropDownListFor() helper method to designate which property you want to bind to and then use the second to supply a collection to populate the actual drop down list with :
<!-- This would bind to the ItemName property, but would populate the list with
the contents of your lstItemImages property -->
#Html.DropDownListFor(m => m.Item.Name, new SelectList(Model.lstItemImages, "Name", "Name"))
Likewise, if you had a collection of values on your J_Items class, you could access those in the second parameter as well:
#Html.DropDownListFOr(m => m.Item.Name, new SelectList(Model.Item.SomeCollection,"Value,"Text")
This assumes that your parent ProductViewer class would be passed into the view.
Related
I am rather new to Razor and I'm using the default templates. I have a ViewModel which contains three models:
public class CarAndOwnerViewModel
{
public IEnumerable<SelectListItem> Cars { get; set; }
public SelectListItem SelectedCar { get; set; }
public OwnerModel Person{ get; set; }
}
I am using this ViewModel in my create method. To get the list of Cars I use:
#Html.DropDownListFor(Model => Model.SeletedCar, new SelectList(Model.Cars, "Value", "Text", Model.Cars))
When I submit this drop down list the value of CarAndOwnerViewModel.SelectedCar is null. I Have a textbox that contains the following and it has it in CarAndOwnerViewModel.Owner.Name:
<input asp-for="Owner.Naame" class="form-control" />
What I filled out in that input box is passed on. I am not sure why the Input Boxes are working but the Select box isn't.
The issue was that the SelectedCar should be an int instead of a SelectedItem
Changing the class to
public class CarAndOwnerViewModel
{
public IEnumerable<SelectListItem> Cars { get; set; }
public int SelectedCar { get; set; }
public OwnerModel Person{ get; set; }
}
Fixed the issue.
I have a trouble with reference to my enum from model in Index view.
Here is my model's code:
public enum UnitOfMeasure {
Item,
Kilogram,
Liter, }
public class Product {
public Product() {
ProductOccurences = new List<ProductOccurence>(); }
[Key]
public int ProductId { get; set; }
public int ProductPhotoId { get; set; }
public UnitOfMeasure? UnitOfMeasure { get; set; }
public string ProductDescription { get; set; }
public virtual ProductPhoto Photo { get; set; }
public virtual ICollection<ProductOccurence> ProductOccurences { get; set; } }
In Index view I have search fields for filtering specific results. You can also search for UnitOfMeasure value (I use #Html.EnumDropDownListFor) - but I can't refer directly to the enum field from my model - because my view is strongly typed:
#model IEnumerable<Models.Product>
To show this field with the values of the selection I use the trick:
#Html.EnumDropDownListFor(model => model.FirstOrDefault().UnitOfMeasure, "Select unit of measure")
but it's terrible and ugly solution - also with unwanted value loaded by default.
What is the most elegant way to solve this issue in my situation?
You can use EnumHelper like this:
#Html.DropDownList("UnitOfMeasure", EnumHelper.GetSelectList(typeof(UnitOfMeasure)))
OR
You can do like this for strongly typed view:
#{ // you can put the following in a back-end method and pass through ViewBag
var selectList = Enum.GetValues(typeof(UnitOfMeasure))
.Cast<UnitOfMeasure>()
.Select(e => new SelectListItem
{
Value = ((int)e).ToString(),
Text = e.ToString()
});
}
#Html.DropDownListFor(m => m.UnitOfMeasure, selectList)
public class State
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Address
{
public State State { get; set; }
}
public class JobSeeker
{
public Address CurrentAddress { get; set; }
}
public class RegisterVM
{
public JobSeeker JobSeeker { get; set; }
public List<State> AllStates { get; set; }
}
in Razor
#Html.DropDownListFor(m => m.JobSeeker.CurrentAddress.State,
new SelectList(Model.AllStates, "Id", "Name" ), " -----Select List----- ")
The result is the drop down is populated with the value present in AllStates, but the problem is m.JobSeeker.CurrentAddress.State is null when posted to controller action. How to set the selected value of dropdown to property m.JobSeeker.CurrentAddress.State
If you change the ViewModel to...
public class RegisterVM
{
public JobSeeker JobSeeker { get; set; }
public List<State> AllStates { get; set; }
public string SelectedState { get; set; }
}
..and have the Drop down use the SelectedState property instead...
#Html.DropDownListFor(m => m.SelectedState,
new SelectList(Model.AllStates, "Id", "Name" ), " -----Select List----- ")
You should then be able to assign it to the State by name.
How is the model binder supposed to map an Id of the state to the model of type State??? If you change your razor view code to:
#Html.DropDownListFor(m => m.JobSeeker.CurrentAddress.State.Id, new SelectList(Model.AllStates, "Id", "Name" ), "--Select--")
Then you will get a non-null instance of state but only the Id property will be populated...
Thanks all of you. I was able to figure out the problem after viewing the Request.Form object which has only one value against State Field i.e SelectedValue of dropdownlist.
As far I understand, it is not possible to set the state property from UI hence I have to use the selected ID of State received from view in the ModelBinder or Controller to set the State Object from db .
I have an MVC application. Here is a part of my code:
View Model:
public class StudentViewModel
{
public int Id { get; set; }
public int? DepartmentId { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public IEnumerable<SelectListItem> AllDepartments { get; set; }
}
View:
#Html.DropDownListFor(model => model.DepartmentId, Model.AllDepartments)
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(StudentViewModel studentViewModel)
{
After the user changes the department selection in the dropdown and submits, for some reason the studentViewModel.DepartmentId contains the old DepartmentId. studentViewModel.Name does contain the new value. What am I missing?
From the comments, you have included another control for property DepartmentId before the dropdownlist. The DefaultModelBinder reads all form values in order (plus route values, query string values etc). Once a property value has been set, any subsequent values for the same property are ignored.
I've searched many posts on SO and still not sure what I did wrong here. I have a model for "Order" which includes a <list> of "OrderItem"
public class Order
{
public int OrderId { get; set; }
public int CustId { get; set; }
public DateTime OrderDate { get; set; }
public int OrderType { get; set; }
...
...
public List<OrderItem> OrderItems = new List<OrderItem>();
}
public class OrderItem
{
public string ProductCode { get; set; }
public decimal RetailPrice { get; set; }
public string ProductQuantity { get; set; }
}
In my view, which is strongly typed to the Order model, I am using an Editor Template to display the order items
#model FTG.Models.Order
#Html.EditorFor(x => x.OrderItems)
and the editor template seems to assign a proper name for the model binding to occur:
input type="number" id="OrderItems_0__ProductQuantity" name="OrderItems[0].ProductQuantity"...
input type="number" id="OrderItems_1__ProductQuantity" name="OrderItems[1].ProductQuantity"...
But my model comes back to the controller with count=0 for the list. The rest of the model looks fine, I just can't get the values from the list of orderitems.
Does anyone know what I am doing wrong, or what I am missing?
I think your model class is missing a property for the child list. Try adding a property for the OrderItems list.
OrderItems is a field, not a property. You need to make it a property with a getter and setter, then in your constructor you create the empty list and assign it to the property.