Gopal Chandak
Gopal Chandak

Reputation: 385

My dropdown selected value is not being posted to the controller

I am new to C# mvc. I have a simple form containing a dropdown list whose values are being populated from a ViewBag

@using (Html.BeginForm("GetSliderValues", "Slider",FormMethod.Post))
{
    @Html.DropDownList("dvalue",(IEnumerable<SelectListItem>)ViewBag.names) 
    <input type="submit" name="submit" value="Submit" />
}

After submitting the form I am trying to get the selected value in the Controller as

[HttpPost]
public ActionResult GetSliderValues()
{
    ProviderName p = new ProviderName();
    p.Name = Request.Form["dvalue"];
    return View(p);   
}

But I am not receiving any value. p.Name is always being set to null.

Upvotes: 0

Views: 974

Answers (1)

PeteMaikel
PeteMaikel

Reputation: 69

Notice that you're not trying to POST data but GET it. That's why you need to use FormMethod.Get instead of FormMehod.Post.

then, just try to create a new SelectList instead of an IEnumerable of SelectListItem.

In your View:

@using (Html.BeginForm("GetSliderValues", "Slider",FormMethod.Get))
{
    @Html.DropDownList("dvalue",new SelectList(ViewBag.names))
    <input type="submit" name="submit" value="Submit" />
}

And then in your controller just Add an string parameter:

public ActionResult GetSliderValues(string dvalue)
{
    ProviderName p = new ProviderName();
    p.Name = dvalue;
    return View(p);   
}

Finally remove The [HttpPost] decorator and it should Work

Upvotes: 2

Related Questions