1

I am struggling to get the selected value from the hard-coded dropdownlist in mvc , below is the code:

View:

<tr><td>No of Helmets</td><td><div class="editor-field">

                <%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
                </div></td></tr>

                 <tr><td>No of Garages</td><td><div class="editor-field">
                 <%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>

Controller:

// dropdown for helmets

 [HttpPost]
    public ActionResult Create(Event trackday, FormCollection formValues)
    {Product product = new Product();//
        ViewBag.mode = "create";

        // for dropdown track
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");
       var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");

        // dropdown for garages
        var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");  product.QtyAvailable = Convert.ToInt32(formValues["garages"]);


        if (ModelState.IsValid)
        {
            trackday.DateAdded = DateTime.Now;
            trackday.DateModified = DateTime.Now;
          //  productResp.Save();//
         //  trackday.Products.Add(product);
            trackdayResp.Add(trackday);
            trackday.Products.Add(product);
            trackdayResp.Save();
            return RedirectToAction("Index");
        }
        else
        {
            return View();
        }


    }`

How can I get the Selected value of the above 2 dropdownlist in mvc post controller.

1
  • Can you show us what you have so far for your post action? Commented Jun 15, 2011 at 10:42

1 Answer 1

3

You've called your dropdowns "helmets" and "garages" if you debug your action and look at the formValues dictionary your should see these two values.

Alternatively you could change your model to have a "Helmets" and "Garages" property of type int? and the model binder should populate these values.

Or you could change your action to something like:

public ActionResult Create(Event trackday, int? helmets, int? garages, FormCollection formValues)

This should be populated with the id's (selected value) of the drop down list.

Update

Here's my code that gets the values either from the collection or from the passed properties:

HTML:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <h2><%: ViewData["helmetsCollectionValue"]%></h2>
  <h2><%: ViewData["helmetsProperty"]%></h2>
  <h2><%: ViewData["garagesCollectionValue"]%></h2>
  <h2><%: ViewData["garagesProperty"]%></h2>
  <% using (Html.BeginForm()) { %>
  <p>
    <%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
  </p>
  <p>
    <%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>
  </p>
  <p>
    <input type="submit" value="Submit" />
  </p>
  <% } %>
</asp:Content>

Controller:

public ActionResult Index()
{
    var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");

    // dropdown for garages
    var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");

    return View();
}

[HttpPost]
public ActionResult Index(FormCollection collection, int? helmets, int? garages)
{
    ViewData["helmetsCollectionValue"] = collection["helmets"];
    ViewData["helmetsProperty"] = helmets;
    ViewData["garagesCollectionValue"] = collection["garages"];
    ViewData["garagesProperty"] = garages;

    var helmetsList = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["helmets"] = new SelectList(helmetsList.ToList(), "Value", "Text");

    // dropdown for garages
    var garagesList = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["garages"] = new SelectList(garagesList.ToList(), "Value", "Text");

    return View();
}
Sign up to request clarification or add additional context in comments.

6 Comments

I cant change it to int coz its a collection
its hsowing nuthing : null :(
does the form collection contain helmets and garages? Can you update you question with your complete view?
I've updated my answer with code showing how it works for me.
it works fine thnx a lot for ur assistance , one more thing to know is , the helemt is link to product type 1 , whereas the garages is link to product type 2 in my db, so what I was looking for if the client select helmet it should add producttype 1 to the db , vice versa..
|

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.