[英]how to get the value of a dropdownlist to the controller in MVC
i am trying to get the value of my dropdownlist to my controller with Jquery, everything i try , gives me a null pointer for the second parameter. 我正在尝试使用Jquery将我的dropdownlist的值传递给控制器,我尝试执行的所有操作都为第二个参数提供了空指针。 Here is my code :
这是我的代码:
this is the jQuery code: 这是jQuery代码:
<script>
$(function () {
$("#ddl").on("change", function () {
var selectedID = $("#ddl").val();
this.href = this.href.replace("idhotel", selectedID);
});
});
</script>
this is my ActionLink: 这是我的ActionLink:
@Html.ActionLink("Voeg toe aan winkelwagentje", "Winkelwagen", new { id = Model.Id, hotelid="idhotel" }, new { @class = "btn btn-default" })
And this is my Action in the Controller: (just header) 这是我在Controller中的Action :(仅标头)
public ActionResult Winkelwagen(int? id,int hotelid)
{
Console.Write("Please give me the hotelid: " + hotelid);
Return View();
}
and here is the dropdownlist: 这是下拉列表:
@Html.DropDownList("ddl", new SelectList(ViewBag.list, "Text", "Value"), "Select")
output dropdownlist: 输出下拉列表:
error: 错误:
Server Error in '/' Application.
“ /”应用程序中的服务器错误。
The parameters dictionary contains a null entry for parameter 'hotelid' of non- > nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult > Winkelwagen(System.Nullable`1[System.Int32], Int32)' > in 'TreinRittenVives.Controllers.ReizenController'.
参数字典包含方法'System.Web.Mvc.ActionResult> Winkelwagen(System.Nullable`1 [System.Int32],Int32)'的参数'hotelid'的null条目,该参数'不可为null的类型'System.Int32' >在“ TreinRittenVives.Controllers.ReizenController”中。 An optional parameter must > be a reference type, a nullable type, or be declared as an optional parameter.
可选参数必须>是引用类型,可为空的类型,或者必须声明为可选参数。 Parameter name: parameters
参数名称:参数
Your second parameter is not nullable on the controller. 您的第二个参数在控制器上不可为空。
it should be... 它应该是...
public ActionResult Winkelwagen(int? id, int? hotelid)
If you want it to be a nullable type. 如果您希望它是可为空的类型。
Regards 问候
Craig 克雷格
Try this: 尝试这个:
$("#ddl").on("change", function () {
var selectedID = $("#ddl").val();
$(a).prop("href", $(a).prop("href").replace("idhotel", selectedID));
});
in your script this.href
means your select element which ID is #ddl
and select
can't contain href
attribute. 在脚本中,
this.href
表示ID为#ddl
且select
不能包含href
属性的select元素。 so you can do something like 所以你可以做类似的事情
<script>
$(function () {
$("#ddl").on("change", function () {
var newHref=$("a").attr("href").replace("idhotel", this.value);
$("a").attr("href",newHref);
});
});
</script>
Your Error: 您的错误:
Server Error in '/' Application. “ /”应用程序中的服务器错误。 The parameters dictionary contains a null entry for parameter 'hotelid' of non- > nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult > Winkelwagen(System.Nullable`1[System.Int32], Int32)' > in 'TreinRittenVives.Controllers.ReizenController'.
参数字典包含方法'System.Web.Mvc.ActionResult> Winkelwagen(System.Nullable`1 [System.Int32],Int32)'的参数'hotelid'的null条目,该参数'不可为null的类型'System.Int32' >在“ TreinRittenVives.Controllers.ReizenController”中。 An optional parameter must > be a reference type, a nullable type, or be declared as an optional parameter.
可选参数必须>是引用类型,可为空的类型,或者必须声明为可选参数。 Parameter name: parameters
参数名称:参数
Reason for error: 错误原因:
The above error will occur most of the time in development and the main reason for the above error, is when we write a action method in the controller , we will specify the parameters as non nullable that is without the question mark in the datatype ex. 上面的错误将在开发过程中的大部分时间发生,并且导致上述错误的主要原因是,当我们在控制器中编写一个动作方法时,我们会将参数指定为不可为空的参数,该参数在数据类型ex中没有问号。
Public void test(int a , int b) 公共无效测试(int a,int b)
The value must be passed, if we couldn't pass for some reason then the above error will occur. 必须传递该值,如果由于某种原因我们无法传递该值,则会发生上述错误。
Solution to Error: The best solution for the above problem is make the parameter as nullable 错误的解决方案:上述问题的最佳解决方案是将参数设置为可为空
Nullable ex: 可为空的ex:
Public void test(int? a,int? b) 公共无效测试(int?a,int?b)
More info : https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/nullable-types/ 更多信息 : https : //docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/nullable-types/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.