0

我有一个下拉框,其中填充了数据库中的数据。如果我在下拉列表中选择一个项目,我想在我的控制器中使用名为 GetValueSelected() 的方法中的值。目前,我无法在 GetValueSelected 方法中获得该值。我怎样才能写这个,以便我可以获得该值并将其用于其他处理。

            @model PopulateDropDownList.Models.Populate
            @using PopulateDropDownList.Models

            @{
                ViewBag.Title = "Index";
            }

            <h2>Index</h2>

            <script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>


             @using (Html.BeginForm("Index", "Home", FormMethod.Post, new {rnctype = "multipart/form-data" }))
             {

                <p>
                    @Html.DropDownList("mylist", Helper.GetDescription(),"--select here--") 
                    <input id="Button1" type="button" value="button" />

                </p>

              }

            <script language="javascript" type="text/javascript">
                $(document).ready(function () {
                    $("#Button1").click(function () {

                        var SelCat = $("#mylist").val();
                        if (SelCat != 0) {
                            var url = '@Url.Action("GetValueSelected", "Home")';
                            $.post(url, { Id: SelCat },
                            function (data) {
                            });
                        }
                        else {
                            alert("You need to select an item");
                        }
                    });
                });   
            </script>


            public class HomeController : Controller
                {
                    public ActionResult Index()
                    {

                        return View();
                    }



                    public void  GetValueSelected()
                    {
                        string getSelectedValue = Request.Form["mylist"];

                    }
                }
4

1 回答 1

0

尝试这个:

CSHTML:

@*rnctype -> enctype*@
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
    <input type="hidden" id="urlGetValueSelected" value="@Url.Action("GetValueSelected", "Home")" />
    <p>
        @Html.DropDownList("mylist", Helper.GetDescription(),"--select here--") 
        <input id="Button1" type="button" value="button" />
    </p>

}

JS:

$(document).ready(function () {
    $("#Button1").click(function (event) {

        event.preventDefault();

        var selCat = $("#mylist").val();
        if (selCat.length != 0) { // check the value if that is correct to empty option 
            $.post($('#urlGetValueSelected').val(), { id: SelCat }, function (data) {
                alert(data.message);
            });
        }
        else {
            alert("You need to select an item");
        }
    });
});   

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {

        return View();
    }

    public JsonResult GetValueSelected(string id)
    {
        string getSelectedValue = id;
        return Json(new { Message = string.Format("id: {0}", id) });
    }
}
于 2013-09-25T16:05:27.817 回答