Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t在局部视图的JQuery中通过ViewBag发送参数。_Javascript_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Javascript Can';t在局部视图的JQuery中通过ViewBag发送参数。

Javascript Can';t在局部视图的JQuery中通过ViewBag发送参数。,javascript,asp.net-mvc,asp.net-mvc-3,Javascript,Asp.net Mvc,Asp.net Mvc 3,我正在开发一个使用razor语法的MVC3应用程序。我正在为评论功能编写部分课程 我的代码是: <script src="../../Scripts/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $('#AddCommentButton').click(function

我正在开发一个使用razor语法的MVC3应用程序。我正在为评论功能编写部分课程

我的代码是:

<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#AddCommentButton').click(function () {
            $.ajax({
                type: 'post',
                url: '/Comment/SaveComments',
                dataType: 'json',
                data:
                { 

                'comments': $('#Comment').val(), @ViewBag.EType, @ViewBag.EId
                 },

                   success: function (data) {

                    $("p.p12").append

                   $('.ShowComments').text('Hide Comments');

                }
            });
        });
    });
</script>

$(文档).ready(函数(){
$('#AddCommentButton')。单击(函数(){
$.ajax({
键入:“post”,
url:“/Comment/SaveComments”,
数据类型:“json”,
数据:
{ 
'comments':$('#Comment').val(),@ViewBag.EType,@ViewBag.EId
},
成功:功能(数据){
$(“第12页”)。追加
$('.ShowComments').text('Hide Comments');
}
});
});
});
在上面的jQuery中,我试图使用ViewBag从视图向控制器发送参数,但它不起作用。我该怎么做呢?

试着这样做:

<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#AddCommentButton').click(function () {
            $.ajax({
                type: 'post',
                url: '@Url.Action("SaveComments", "Comment")',
                data: { 
                    comments: $('#Comment').val(), 
                    etype: @Html.Raw(Json.Encode(ViewBag.EType)), 
                    eid: @Html.Raw(Json.Encode(ViewBag.EId))
                },
                success: function (data) {
                    $("p.p12").append(data);
                    $('.ShowComments').text('Hide Comments');
                }
            });
        });
    });
</script>
或定义视图模型:

public class SaveCommentViewModel
{
    public string Comments { get; set; }
    public string EType { get; set; }
    public string EId { get; set; }
}
然后:

[HttpPost]
public ActionResult SaveComments(SaveCommentViewModel model)
{
    ...
}

脚本中呈现了哪些值?你能给我们看看结果吗?谢谢,但我不想作为视图模型传递,我想作为单独的参数发送。像这样
publicjsonresult SaveComments(字符串注释、字符串EType、字符串EId)
不起作用。。。我按原样输入了smae<代码>注释:$('#Comment').val(),etype:@Html.Raw(Json.Encode(ViewBag.etype)),eid:@ViewBag.eid显示警告-'{'应在注释附近,';'应在词组附近。您能否更新您的问题并发布您的实际代码,因为我的答案中的代码应该有效。您是否注意到我使用了
@Html.Raw(Json.Encode(ViewBag.EId))
因为显然
ViewBag.EId
在您的案例中是一个字符串值。您没有在问题中提供此信息,所以我假设它是一个整数。我还在初始答案中留下了一条注释,说明如果此值是字符串,则需要对其进行编码。很抱歉,您的代码起作用了,我在代码中犯了一些愚蠢的错误……谢谢It’谢谢你的努力。。。
[HttpPost]
public ActionResult SaveComments(SaveCommentViewModel model)
{
    ...
}