1

I have here a scenario. I want to make an HTTP POST action in the form so here's how I did it.

public class Item
{
  public Item()
  {
    Storages = new HashSet<Storage>();
  }
  public int Id { get; set; }
  public string Name { get; set; }

  public virtual ICollection<Storage> Storages { get; set; }
  -- remove some lines for brevity --
}

public class Storage
{
  public int Id { get; set; }
  public string Name { get; set; }
  --- remove some lines for brevity --
}

So basically, An Item has many Storage And so I created viewmodel.

public class CreateStockViewModel
{
  public string Name { get; set; }
  public int StorageId { get; set; }
  -- remove some lines for brevity --
}

In my Controller. I have this

[HttpGet]
public ActionResult Create()
{
  ViewBag.Storages = _storageService.All
                       .OrderBy(i => i.Name)
                       .ToSelectList(s => s.Name, s => s.Id);

  return View();
}

In my View:

@model Wsfis.Web.ViewModels.ItemViewModels.CreateStockViewModel

@Html.DropDownList("Storages")

Now my problem is, when I submit the form. And have Quick Watch to the model being passed. It is Null or 0

public ActionResult Create(CreateStockViewModel item)
{
  // some code
}

In a nutshell,

  1. When I submit the form all fields are being bind except for the @Html.DropDownList. Where did I missed?

Some additional side note:

  1. They say Views should be strongly typed. Then what should I pass in View then? (A sample code would be great. Thanks)

As for the ToSelectList method I copy this code (I hope it's alright)

Any help would be much appreciated. Thanks.

1
  • 1
    the problem is that you named your dropdownlist StoreHouses while the value being bounded to the model is called StorageId. If you inspect your dropdownlist using the browser utilities or firebug you'll see that the name attribute of the select tag is StoreHouses. When this is submitted the mvc binder has no clues on how to bind the value to the model because the CreateStockViewModel has no property with such name. Commented Jun 30, 2014 at 6:03

2 Answers 2

1

Your form input has a different name to your property so the default model binder doesn't know how to bind your model.

You could pass in a different name to use to the DropDownList helper, however I prefer to use the strongly typed helpers:

@Html.DropDownListFor(m => m.StorageId, ViewBag.Storages as IEnumerable<SelectListItem>)
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry. That's a typo. That should be Storages. Anyway, can you demonstrate on how to do stronglytyped helpers?
My example above DropDownListFor is using a strongly typed helper. All the ...For helpers that take in a lambda expressions are strongly typed.
0

Try like this:

 ViewBag.StorageId = _storageService.All
                       .OrderBy(i => i.Name)
                       .ToSelectList(s => s.Name, s => s.Id);

in view:

@Html.DropDownList("StorageId")

it will now post the drop down list selected value in CreateStockViewModel object's StorageId property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.