MVC3传递模型视图用javascript控制器 [英] MVC3 Pass Model view to controller using javascript

查看:110
本文介绍了MVC3传递模型视图用javascript控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从视图模型使用javascript控制器。但它返回空值。

I am trying to pass model from view to controller using javascript. But it returns null values.

下面是我的看法
Newcamp.chstml

 <input type="submit" value="Preview" id="ClickMe" class="cssLoginButton blue"/>

<script type="text/javascript">
 function OpenWindow(query, w, h, scroll) {
     var l = (screen.width - w) / 2;
     var t = (screen.height - h) / 2;

     winprops = 'resizable=0, height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + 'w';
     if (scroll) winprops += ',scrollbars=1';
     var f = window.open(query, "_blank", winprops);
 }


 $(function () {
     $('#ClickMe').click(function () {
             OpenWindow('@Url.Action("NewCampEmail", "Camp",new{model = @Model})', 700, 700, true);
     });
 });
</script>

我的控制器操作的 NewCampEmail

    [HttpGet]
    [OutputCache(Location = OutputCacheLocation.None)]
    [Authorize]
    public ActionResult NewCampEmail(Models.NewCamp model)
    {            
        return ReturnModelWithEmailType(model,EmailType.NewCampEmail);
    }

模式我得到空值。为什么我得到空值?有没有在我的code什么变化吗?我的模型是一样的 NewCamp.chstml 即Models.Newcamp

In model I get null values. Why I am getting null values? Is there any change in my code? My model is same in NewCamp.chstml i.e. Models.Newcamp

推荐答案

您无法通过JavaScript以这种方式传递@Model。模型只存在于服务器端,并在创建页面前面的HTTP响应发送回的使用。当然,你可以从模型价值,并派他们为contants到控制器,但没有办法引用每本身的实际模型这种方式。

you can't pass the @Model via javascript in this way. the Model ONLY exists on the serverside and is used in page creation ahead of the http response sending it back. You can of course get 'values' from the model and sent them as contants to the controller but there's no way to reference the actual Model per-se in this way.

您最好的选择是看 knockoutjs (一个伟大的锻炼; Tibial无关)或类似的,如果你想参考一个模式,而不是在页面上的属性等。

Your best bet would be to look at knockoutjs (a great excercise irrespective) or similar if you want to refer to a single Model, rather than properties on the page etc.

在简单地说,一旦页面已经呈现,在@Model不见了,只能通过网页属性来引用或由@Model填充JavaScript对象...

In a nutshell, once the page has been rendered, the @Model is gone and can only be referenced via page properties or javascript objects populated by the @Model...

这篇关于MVC3传递模型视图用javascript控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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