jQuery javascript asp .net mvc razor 中的访问模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21391402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:33:01  来源:igfitidea点击:

access model in javascript asp .net mvc razor

jqueryasp.net-mvcrazor

提问by SARAVAN

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

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

I am trying to access my model in javascript as follows

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

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

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

但没有任何作用。我收到模型未定义错误

回答by David

modelis 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 ofmix 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.

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

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

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

Model.SomeProperty

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

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

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

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 Modelclient-side. So you need to indicate that there's server-side pre-processing to do:

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

alert(@Model.SomeProperty)

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

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

alert('@Model.SomeProperty')

Thus, the server-side value of SomePropertywill 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:

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

alert('Hello World')

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/HTML/CSS 都只是一个大字符串。视图本质上只是创建一个大字符串发送到浏览器。一旦它在浏览器中,客户端渲染就会知道 JavaScript/HTML/CSS 之间的区别,并在服务器端代码消失很久之后相应地执行。