开发者

how do i pass value to controller

I have a drop down where I want to pass the selected value I selected to the controller. How do I ac开发者_开发知识库complish this?


Controller:

public class TestController : Controller
{
    public ActionResult Test(Test input)
    {
        string selected = input.YourDropDown; //Here is your value

        return View();
    }

}

Model:

public class Test
{
    public string YourDropDown { get; set; }
}

View:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<###NAMESPACE###.Models.Test>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test</title>
</head>
<body>
    <div>
        <% using (Html.BeginForm()) { %>
            <%= Html.DropDownListFor(model => model.YourDropDown, new[] { new SelectListItem { Text = "Test", Value = "Value" }}) %>
            <input type="submit" />
        <% } %>
    </div>
</body>
</html>

Put the view in the folder like: /Views/Test/Test.aspx

and your url will be:

url/Test/Test

This would properly be one of the better and most extensible ways to do what you what. You could avoid to make the model class and use Html.DropDownList instead of Html.DropDownListFor.


You can pass via the querystring like so:

http://YourMvcApp/YourController/YourAction/?yourvariable=yourvalue

Then inside your Action you can get to the value by doing

Dim myValue As Object = Request("yourvariable")

Or in C#

Object myValue = Request["yourvariable"];

HTH,

Mike

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜