0

ViewModel

namespace VendorFinancing.Web.Models
{
    public class CreateApplicationViewModel
    {
        public Application Application { get; set; }

        public List<Equipment> Equipments { get; set; }

        public string SelectedVendorId;


        public IEnumerable<SelectListItem> VendorList { get; set; }
    }
}

Controller

public ActionResult Create()
        {
            var model = new CreateApplicationViewModel();



            model.VendorList = new List<SelectListItem>() { new SelectListItem() { Text = "London", Value = "1" }, 
                        new SelectListItem() { Text = "New York", Value="2" } };

            return View(model);
        }


[HttpPost]
        public ActionResult Create(CreateApplicationViewModel model)
        {

            return View(model);
        }

View

@using (Html.BeginForm("Create", "Application", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{

    <div class="form-horizontal">
        <h2>Submit Application </h2>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <h3>Pick Vendor</h3>

        <div class="form-group">
            <div class="col-md-2">
                @Html.LabelFor(c => c.SelectedVendorId)
            </div>

            <div class="col-md-10">
                @Html.DropDownListFor(model => model.SelectedVendorId, Model.VendorList)
            </div>

        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10 pull-left">
                <input type="submit" value="Create" class="btn btn-default pull-left" />
            </div>
        </div>
    </div>
}

This is very basic create. When form is submitted SelectedVendorId is still null. I tried the same example in different it is still working. I am not able to figure out the reason. Any help is appriciated.

EDIT: found my mistake SelectedVendorId didnt have get and set. After making it a property it worked.

Praneeth
  • 2,527
  • 5
  • 30
  • 47
  • http://stackoverflow.com/questions/7142961/mvc3-dropdownlistfor-a-simple-example, http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx, http://agilewarrior.wordpress.com/2012/12/13/how-to-simple-html-dropdownlistfor-mvc-net/ – Jack Sep 28 '14 at 03:19
  • Assuming you have Firebug or similar installed, double-check the actual POST request; it could be that the value is posted but is not being picked up by the model binder. – Tieson T. Sep 28 '14 at 03:20
  • Is the dropdown disabled when you submit the form? – ekad Sep 28 '14 at 03:21
  • @ Tieson: let me check fiddler. @ekad: how can dropdown be disabled? – Praneeth Sep 28 '14 at 03:27
  • I was thinking that there might be a javascript that disables the dropdown so the selected value won't be posted. – ekad Sep 28 '14 at 03:31
  • I checked the value in fiddler i can see the selected value in the post data but it is not binding to the form model on the controller side. – Praneeth Sep 28 '14 at 03:35
  • 1
    I found my mistake. My viewModel selectedVendorId didnt have get and set. what a stupid mistake. – Praneeth Sep 28 '14 at 03:39

0 Answers0