Click here to Skip to main content
14,869,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to MVC3. I have created a static dropdown list box in View

XML
<div class ="editor-field">
     <select name="quota" id="list">
           <option value="1">General</option>
           <option value="2">Tatkal</option>
           <option value="3">Ladies</option>
    </select>
</div>


Now I want to pass the selected value to another page using ViewBag.
I don't know what are the things I need to have in Model and Controller pages.

Please help.
Posted
Comments
Jameel VM 30-Jul-13 10:09am
   
why you are trying to pass values using viewbag?
srmohanr 30-Jul-13 13:52pm
   
I am sorry, I don't know how to pass the value to another page.
I thought ViewBag can be used to pass value.
If there is any other way available, please let me know

1 solution

I think that the best way yo achieve this by using ViewModel.

Create two class QuotaViewModel and Quota
C#
public class QuotaViewModel
{
public int QuotaId{get;set;}
public List<quota> Quotas{get;set;}
}
public class Quota
{
public int Id{get;set;}
public string Name{get;set;}
}</quota>



Create a controller and two ActionResults

C#
public ActionResult Index()
{
var quota=new List<quota>{new Quota{Id=1,Name="Tatkal"},new Quota{Id=2,Name="Ladies"}};

return View(quota);
}
</quota>

Create a action for get the value of dropdownlist like below
C#
public ActionResult Create(QuotaViewModel quotaViewModel)
{
//you get the selected id while clicking the button
int selectedId=quotaViewModel.QuotaId
return View();
}


Create a view which should return the model QuotaViewModel

C#
@model QuotaViewModel
@{
}
@using(Html.BeginForm("YourControllerName","Create", FormMethod.Post)){
@Html.DropDownListFor(m => m.QuotaId, Model.Quotas)
<input type="submit" value="click here"/>
}

Hope this helps
   
v3
Comments
srmohanr 30-Jul-13 15:00pm
   
Hi,thanks for your help.

Now I am getting an error "Cannot convert lambda expression to type 'string' because it is not a delegate type" in the line, @Html.DropDownList(m => m.QuotaId, Model.Quotas);

How to rectify this. Please help
Jameel VM 31-Jul-13 1:24am
   
put @ before html.DropdownListFor.I have modify my answer
Jameel VM 31-Jul-13 1:27am
   
after working with sample ofcourse you will get an idea of how to pass values from view to actionresults. I recommend you to understand the basic ASP.NET MVC. this link will help you.
http://www.asp.net/mvc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900