为什么@Model尚未通过jquery脚本传递给控制器​​的action参数? [英] Why @Model haven't been passed to controller's action parameter by jquery script?

查看:103
本文介绍了为什么@Model尚未通过jquery脚本传递给控制器​​的action参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Details.cshtml(.../Person/Details/id)视图中@ModelPerson对象传递给PersonController中的Test操作,该操作返回部分视图Test.cshtml.

I wanted to pass Person object which is @Model in Details.cshtml(.../Person/Details/id) view to the Test action in PersonController which returns partial view Test.cshtml.

局部视图可以通过按钮展开/折叠,因此我使用脚本.一切正常,我尝试传递给Test@Model动作未传递->在Test动作内为空

The partial view is expandable/collapsable by the button so I use script. Everything works beside that @Model I try to pass to Test action is not passed-> it is null inside the Test action

为什么没有通过(模型)以及如何通过它?

Why it(model) hasn't been passed and how pass it?

完整代码:

脚本:

<script>
    function BtnOnclick() {
        $.ajax({
            type: 'POST',
            url: '@Url.Content("~/Person/Test")', 
            data: {
                person: '@Model' //HERE I TRY TO PASS PERSON
            },
            success: function (data) {
                $('#divpopup').css("display", "block");
                $('#btnExpand').css("display", "none");
                $('#divpopup')[0].innerHTML = data;
            }
        });
    }
    function CollapseDiv() {
        $('#divpopup').css("display", "none");
        $('#btnExpand').css("display", "block");
    }
</script>

详细信息视图:

@model WebApplication2.Models.Person
[...]
<p>
    <input type="button" value="Expand" id="btnExpand"
           onclick="javascript:BtnOnclick();" />
</p>
<div id="divpopup" style="display:none">

</div>

控制器的动作:

        [HttpPost]
        public ActionResult Test(Person person) {
           // ViewBag.Chk = parentEls;
            System.Diagnostics.Debug.WriteLine("PASSED: ");
            System.Diagnostics.Debug.WriteLine(person.FirstName); // IT IS NULL
            return PartialView(person);
        }

局部视图:

@model WebApplication2.Models.Person
<hr />
<h2>Survey 1</h2>

<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
<dt>
    @Html.DisplayNameFor(model => model.Notes)
</dt>
<dd>
    @Html.DisplayFor(model => model.Notes)
</dd>
<hr />

已崩溃:

已扩展:

如果我这样做,那很不好吗

Is it bad if I just do:

在脚本中:

 data: {
               id: '@Model.Id'
            },

实际情况:

 [HttpPost]
        public ActionResult Test(int id) {
            System.Diagnostics.Debug.WriteLine(id);
            Person person = db.Persons.Find(id);
            System.Diagnostics.Debug.WriteLine("PASSED: ");
            System.Diagnostics.Debug.WriteLine(person.FirstName);

            return PartialView(person);
        }

推荐答案

基于注释,似乎不需要进行AJAX调用来获取渲染主视图时已经可以访问的数据.另一种选择是在<div>中呈现Notes值,并使用<button>切换可见性,因此在主视图中

Based on comments, it seems unnecessary to make AJAX calls to get data that you already have access to when rendering the main view. An alternative could be to render the Notes value in a <div> and toggle the visibility using a <button>, so in the main view

@model WebApplication2.Models.Person

... // other display properties for Person

<button id="toggle"></button>
<span id="survey">Survey</span>
<div id="notes">@Html.DisplayFor(m => m.Notes)</div>

和脚本

$('#toggle').click(function() {
  $('#notes').toggle();
  $(this).toggleClass('expanded');
});

请注意,按钮的类名也被切换为将按钮的外观从展开"更改为折叠"(类似于树形视图控件)

Note the class name of the button is also toggled to change the appearance of the button from 'expanded' to 'collapsed' (similar to a treeview control)

引用小提琴

这篇关于为什么@Model尚未通过jquery脚本传递给控制器​​的action参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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