简体   繁体   English

如何在局部视图中使用MVC模型视图属性设置敲除属性

[英]How to set a knockout property with a MVC model view property in a partial view

i have the next scenario: 我有下一个场景:

Im working with knockout (ver 2.1.0) and ASP MVC.NET, so in one specific point my controller calls a new view. 我正在使用敲除(2.1.0版)和ASP MVC.NET,因此在一个特定点上,我的控制器调用了一个新视图。 This view renders a partial view with the next call: 此视图使用下一个调用呈现局部视图:

<div id="productSeriesNonSocks">
    @{
    Html.RenderPartial("~/Views/SalesOrderManagement/GetAvailableProductSeriesNonSocks.cshtml", Model);
     }
</div>

The controller called the view with a viewModel (Model) with a few properties, so the above call sends the whole model to the partial view. 控制器使用带有一些属性的viewModel(Model)调用了视图,因此上述调用将整个模型发送到局部视图。

In the partial view I can access the model properties with a simple sentence like this: 在局部视图中,我可以使用以下简单语句访问模型属性:

Available season type: @Model.SeasonType

The problem starts here... the partial view has it own knockout model: 问题开始于此...局部视图具有其自身的剔除模型:

<script type="text/javascript">
$(function () {
    //activates KO
    ko.applyBindings(productSeriesNonSocksModel);
    GetDataFromModels();
});

var productSeriesNonSocksModel = {
    AvailableProductSeriesNonSocks: ko.observableArray([]),
    selectedProdSeries: ko.observable(),
    seasonType: ko.observable()
};

function GetDataFromModels() {
    $.get('/SalesOrderManagement/GetAvailableProductSeriesNonSocks', { seasonType: this.seasonType() }, function (data) {
        productSeriesNonSocksModel.AvailableProductSeriesNonSocks(data.AvailableProductSeriesNonSocks);
    });
}

</script>

the GetDataFromModels() function should call a method in the controller with the seasonType as a parameter in order to retrieve the proper array and put it on the AvailableProductSeriesNonSocks array. GetDataFromModels()函数应使用季节类型作为参数调用控制器中的方法,以便检索适当的数组并将其放在AvailableProductSeriesNonSocks数组上。 The problem here is that I don't know how to pass the value from a property model (in this case @Model.SeasonType) to the knockout model to call the function in the controller (productSeriesNonSocksModel.seasonType). 这里的问题是,我不知道如何将值从属性模型(在本例中为@ Model.SeasonType)传递给敲除模型,以调用控制器中的函数(productSeriesNonSocksModel.seasonType)。

I tried to pass the value with: 我试图通过传递值:

seasonType: ko.observable(@Model.SeasonType)

or 要么

seasonType: @Model.SeasonType

but it doesn't work. 但这不起作用。 Any idea to pass a model property value to the knockout property value in order to make the call with the $.get(...)? 为了将$ .get(...)进行调用,将模型属性值传递给敲除属性值的想法吗?

To clarify, I can't just ask the value because that value (SeasonType) was choosen by the user in the previous view. 为了澄清,我不能只问这个值,因为该值(SeasonType)是用户在上一个视图中选择的。

Thank you in advance. 先感谢您。

You can use razor in script tags. 您可以在脚本标签中使用剃刀。

<script type="text/javascript">
    var seasonType = @Model.SeasonType;
    // do something with season type
</script>

It might also make sense to avoid the additional request. 避免附加请求也很有意义。 You could put the data from "/SalesOrderManagement/GetAvailableProductSeriesNonSocks" in the model or the ViewBag and render that as JavaScript Object. 您可以将来自“ / SalesOrderManagement / GetAvailableProductSeriesNonSocks”中的数据放入模型或ViewBag中,并将其呈现为JavaScript对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM