0

I want to add a dropdownlist in my form, with 2 values userid and username in my dropdownlist, and also I want to get the value selected by the user when I click the button. I'm new to MVC and so far, I have not worked on dropdownlist, tried few samples but nothing seems to be working the way I want.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
always v
  • 275
  • 1
  • 3
  • 9
  • Well, it won't be a dropdownlist, but a ` – Andre Calil Jul 30 '12 at 05:30
  • Look here, there's a similar example http://stackoverflow.com/questions/10649081/howcome-i-populate-selectlist-with-list-of-categories – lexeme Jul 30 '12 at 05:33
  • Show us the samples that didn't work, you will know where you are going wrong ! ! – V4Vendetta Jul 30 '12 at 05:33
  • i followed the below example, but getting the error as object reference not set to and instance of an object http://stackoverflow.com/questions/7142961/mvc3-dropdownlistfor-a-simple-example – always v Jul 30 '12 at 05:37
  • @alwaysv Follow up: if you got the answer you were looking for, don't forget to mark it as correct. Regards. – Andre Calil Jul 31 '12 at 01:25

4 Answers4

0

I'll jump lots of MVC3 concepts. If you're really new to ASP.NET MVC, you should take a look at some tutorials.

This code should help you:

VIEW

    @using (Html.BeginForm("ACTION NAME", "CONTROLLER NAME"))
    {
        <select name="select">
            <option value="username" selected>User name</option>
            <option value="userid">User id</option>
        </select>
        <input type="submit" />
    }

ACTION

[HttpPost]
public ActionResult ACTIONNAME(string select)
{
    //...
}

Please, note:

  • ACTION NAME and CONTROLLER NAME at the BeginForm helper. You will have to modify this at your code
  • The select name ("select") and the name of the argument at the action ("select"). This is not a coincidence, it's a convention. MVC uses the name attr to bind data
  • The selected attribute at the option will make it the default option

Regards

Andre Calil
  • 7,652
  • 34
  • 41
  • where is the dropdown here? @html.dropdownlist? – always v Jul 30 '12 at 05:43
  • @alwaysv I'm *not* using a HTML helper (such as `@Html.DropDownList`) to make sure that you understand that MVC is all about the markup, and that you're free to write it your own way. Also, you didn't said you wanted it using a HTML helper =) – Andre Calil Jul 30 '12 at 05:46
  • yes,thanks a lot, this is working fine, and its simple,can u please tell how can i get the selected value?, let me be clear, i need to get selected value to my actionresult, based on that i wil perform some database operations – always v Jul 30 '12 at 05:46
  • @alwaysv If you run this code, you'll notice that, at the **action**, `select` (the argument) will have the value selected by the user – Andre Calil Jul 30 '12 at 05:49
  • yes, also how can i set default value as select for this dropdown? – always v Jul 30 '12 at 05:53
  • 1
    @alwaysv Updated. I'd **strongly** recommend you to learn some HTML concepts. *W3Schools* and *Mozilla Developer Network* are great places for that. – Andre Calil Jul 30 '12 at 05:56
0

See one of the ways you can do it is send the list in a model property as the binding and for the value you can bind it to another property like :

public class YourModel 
{    
public List<UserList> OptionList { get; set; }
public String YourValue{get;set;}
}
public class UserList
{
   public String UserName{get;set;}
   public String UserId{get;set;}
}

@Html.DropDownListFor(model => model.YourValue, Model.OptionList, "")

In the helper there are overided options which are used to specify the value and text.

And Remember :
This is StackOverflow. Even the Not working example which you have tried are important for the ones who try to help you since they are spending their precious bandwidths for u.

bhuvin
  • 1,382
  • 1
  • 11
  • 28
0

You don't need create a new model class for each view, just put this on controller:

ViewBag.FieldName = new SelectList(new List<SelectListItem>() {
    new SelectListItem { Value = "userid", Text = "User ID" },
    new SelectListItem { Value = "username", Text = "User name" }
});

And this on view:

@Html.DropDownList("FieldName")
0
  1. You need to create a collection of SelectListItem like:

    IEnumerable<SelectListItem> selectList =
    from c in areaListResponse.Item
    select new SelectListItem
    {
      Text = c.AreaName,
      Value = c.Id.ToString()
    };
    
  2. Pass this selectList to your view:

    return View(selectList);
    
  3. In your cshtml:

    @model IEnumerable<SelectListItem>
    
    @Html.DropDownListFor(m => m.RequestAreaName, Model)
    
  4. If you need complecated object, you may need a wrapper class like:

     public class RaiseRequestModelWrapper
    {
    public IEnumerable<SelectListItem> GetModel { get; set; }
    public RaiseRequestModel PostModel { get; set; }
    }
    
Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
Edi Wang
  • 3,547
  • 6
  • 33
  • 51