javascript asp .net mvc razor 中的访问模型 [英] access model in javascript asp .net mvc razor

查看:26
本文介绍了javascript asp .net mvc razor 中的访问模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 razor 开发 asp .net mvc 项目.

I am working on asp .net mvc project with razor.

我正在尝试使用 javascript 访问我的模型,如下所示

I am trying to access my model in javascript as follows

警报(模型.安全配置文件);警报(模型.安全配置文件);警报(@Model.SecurityProfile);警报(@model.SecurityProfile);

alert(Model.SecurityProfile);
alert(model.SecurityProfile);
alert(@Model.SecurityProfile);
alert(@model.SecurityProfile);

var SecurityProfileViewModel = {视图模型:模型,Id:SecurityProfileId,ProfileId: $('#ProfileId').val(),JobTitleId: $('#JobTitle').val(),SecurityProfileTypeId: $('#SecurityProfileType').val(),状态:$('#ddlStatus').val(),原因:$('#txtReason').val(),模式:$('#hidMode').val()};

var SecurityProfileViewModel = { ViewModel: model, Id: SecurityProfileId, ProfileId: $('#ProfileId').val(), JobTitleId: $('#JobTitle').val(), SecurityProfileTypeId: $('#SecurityProfileType').val(), Status: $('#ddlStatus').val(), Reason: $('#txtReason').val(), Mode: $('#hidMode').val() };

    $.ajax({
        url: '/SecurityProfile/Edit',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(SecurityProfileViewModel),
        success: function (data) {
            alert(data);
            window.location.href = '/SecurityProfile/Index';

        }
    });

但没有任何效果.我收到模型未定义错误

But nothing works. I am getting model is undefined error

推荐答案

model 是未定义的,就 JavaScript 而言.您视图中的服务器端代码在服务器端执行.JavaScript 没有这个概念.它只关心该代码的客户端输出.您可以有点将两者混合,但需要记住服务器端组件只是为了发出字符串,这些字符串将成为客户端输出的一部分.

model is undefined, as far as JavaScript is concerned. The server-side code in your view executes, well, server-side. JavaScript has no notion of that. It's only concerned with the client-side output of that code. You can kind of mix the two, but need to keep in mind that the server-side components are just there to emit strings which will be part of the client-side output.

因此,例如,如果您的模型中有一个名为:

So, for example, if you have a property on your model called:

Model.SomeProperty

那么你不能像这样直接在 JavaScript 中使用它:

Then you can't use it directly in JavaScript like this:

alert(Model.SomeProperty)
// or
alert(SomeProperty)

这不是使用 razor 视图语法来告诉视图引擎这里有服务器端代码.这是语法上的客户端代码,没有 Model 客户端.所以你需要指出有服务器端预处理要做:

That's not using the razor view syntax to tell the view engine that there's server-side code here. This is syntactically client-side code, and there is no Model client-side. So you need to indicate that there's server-side pre-processing to do:

alert(@Model.SomeProperty)

此外,如果 SomeProperty 是一个字符串,那么请记住它的 输出 不会包含引号.因此,您还需要为客户端代码提供这些:

Additionally, if SomeProperty is a string, then keep in mind that it's output isn't going to include quotes. So you'd need to provide those for client-side code as well:

alert('@Model.SomeProperty')

因此,当 SomeProperty 的服务器端值呈现给客户端时,将在此处发出.因此,如果该值类似于 "Hello World",那么生成的客户端代码将是:

Thus, the server-side value of SomeProperty will be emitted here when it's rendered to the client. So if the value is something like "Hello World" then the resulting client-side code would be:

alert('Hello World')

最主要的是要记住服务器端代码和客户端代码之间的分离.就服务器端代码而言,所有 JavaScript/HTML/CSS 都只是一个大字符串.视图本质上只是创建一个大字符串发送到浏览器.一旦进入浏览器,客户端渲染就会知道 JavaScript/HTML/CSS 之间的区别,并在服务器端代码消失很久之后相应地执行.

The main thing is to keep in mind the separation between the server-side code and the client-side code. All JavaScript/HTML/CSS is just one big string as far as server-side code is concerned. The view is essentially just creating a big string to send to the browser. Once it's in the browser, the client-side rendering knows the difference between JavaScript/HTML/CSS and executes accordingly, long after the server-side code is gone.

这篇关于javascript asp .net mvc razor 中的访问模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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