从视图的javascript向控制器发送参数(不能为null) [英] Send a parameter (cannot be null) to the controller from the javascript of the view

查看:62
本文介绍了从视图的javascript向控制器发送参数(不能为null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更改视图中的下拉列表时调用以下函数 GetProject



Javascript



The following function GetProject is called on change of the dropdown in the view.

Javascript

<script>
    function GetProject(projId) {
        
        var selectedText = projId.options[projId.selectedIndex].innerHTML;

        var selectedValue = projId.value;
        
        var url = '@Url.Action("OpenEndedDetails", "ResponseController", new { @projId = "projId" }, null);
        url = url.replace('projId', encodeURIComponent(selectedValue));

        alert(url);
        window.location.href = url;
    }

</script>





ResponseController



ResponseController

public ActionResult OpenEndedDetails(int projId)
        {
            
            ResponseContext feedback = new ResponseContext();
           
            /* Conditions:
             * @All openended questions in a project
             * */
            List<Response> feedbacks = feedback.Responses.Where(resp => resp.Answer.Equals("0") && resp.ProjectID == projId).ToList();

            return View(feedbacks);
        }





错误:

参数字典包含参数'projId'的空条目对于'SmartAdminMvc.Controllers.ResponseController'中方法'System.Web.Mvc.ActionResult OpenEndedDetails(Int32)'的非可空类型'System.Int32'。可选参数必须是引用类型,可以为空的类型,或者声明为可选参数。

参数名称:参数





ProjId 是int类型的主键。我不知道如何摆脱这种异常。我希望Url.Action加载我的整个视图,以及从下拉列表中传递的值。

我可能在代码中犯了一些重大错误。请更正。

请帮助我,因为我长期坚持这个问题。

谢谢



Error:
The parameters dictionary contains a null entry for parameter 'projId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult OpenEndedDetails(Int32)' in 'SmartAdminMvc.Controllers.ResponseController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters


ProjId is a primary key of type int. I don't know how to get rid of this exception. I want the Url.Action to load my entire view, with the value that i pass from the dropdown.
I might have made some major blunders with code. Kindly correct it.
Please help me as i am stuck with this issue for long.
Thanks

推荐答案

Check whether value is null before assigning it to selectedValue
to check whether value is null refer this





http://stackoverflow.com/questions/6003884/如何在javascript中检查空值 - [ ^ ]


这篇关于从视图的javascript向控制器发送参数(不能为null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆