如何获取HTML下拉列表值从视图到MVC中的控制器?

时间:2018-09-11 18:12:52

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

需要小帮助

1)我在MVC视图中创建了这样的html Dropdown

<select name="Associateddl" id="Associateddl"></select> 

2)我正在使用类似Jquery Ajax的选项添加到下拉列表中

$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'customservice.asmx/getallmember',
dataType: 'JSON',
success: function (response) {
var getData = JSON.parse(response.d);
console.log(getData.length);
if (getData.length > 0) {
$("#msg").hide();
$.each(getData, function (i, item) {
var optiontext = "";
optiontext = "<option value='" + item.Aid + "'>" + item.AMail + "</option>";
$("#Associateddl").append(optiontext);
});
}
else {
$("#msg").append("<p style='color:red'><b>Currently there are no members ,Please Add !!</b></p>");
}
},
error: function (err) {
//alert(err);
}
});

3)现在,我想从View检索下拉选择的值到控制器。

我的模特:

public class Event
{
[DisplayName("Event Name")]
[Required(ErrorMessage ="Event Name is Mandatory...")]
public string Eventname { get; set; }
[DisplayName("Event Year")]
[Required(ErrorMessage ="Enter the Year...")]
public int Year { get; set; }
[DisplayName("Associate ID")]
[Required(ErrorMessage ="Associate ID is required...")]
public string Associateddl { get; set; }
}

我的控制器:

[HttpPost]
public ActionResult Index(Event eve)
{

string ename = eve.Eventname;
int eyr = eve.Year;
string eassociate = eve.Associateddl; //Here i want to retrive the dropdownselected Value

return View();
}

请帮助我从View获取“ Html”下拉列表的选定值。

1 个答案:

答案 0 :(得分:1)

这是一个有关如何在控制器中查看ddl值的示例。我将为您提供一个ASP.NET提琴,您可以单击该提琴将托管解决方案。

网络服务

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public string getallmember()
    {
        var drop2 = new List<SelectListItem>();
        SelectListItem sli1 = new SelectListItem { Text = "MoreOptions1", Value = "1" };
        SelectListItem sli2 = new SelectListItem { Text = "MoreOptions2", Value = "2" };
        drop2.Add(sli1);
        drop2.Add(sli2);
        //GET NewtonSoft
        var json = JsonConvert.SerializeObject(drop2);
        return json;
    }

控制器

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Tut118(string Associateddl)
    {
        //put breakpoint here
        return View();
    }

    public ActionResult Tut118()
    {
        return View();
    }

查看

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut118</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#theButton").click(function () {
               $.ajax({
                    type: "Post",
                    url: "customservice.asmx/getallmember",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        $("#Associateddl").empty();
                        var theData = JSON.parse(response.d);
                        $.each(theData, function (i, anObj) {
                            $("#Associateddl").append($('<option>').text(anObj.Text).attr('value', anObj.Value));
                        });
                    }
                    ,
                    error: function (request, status, error) {
                        alert(error);
                    }
                });
            })
        })
    </script>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            <input type="button" id="theButton" value="click to get ddl" />
            <select name="Associateddl" id="Associateddl"></select>
            <input type="submit" value="click after selecting ddl" />
        }

    </div>
</body>
</html>