如何从模型中获取的数据为JavaScript MVC 4? [英] How to get Data from Model to JavaScript MVC 4?

查看:164
本文介绍了如何从模型中获取的数据为JavaScript MVC 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的功能:

 <脚本>计算功能()
     {
         VAR ITEMPRICE =的document.getElementById(价格);
         VAR体重=的document.getElementById(权重);
         VAR SelWeight = weight.options [weight.selectedIndex] .value的;
         警报(SelWeight);
         VAR类别=的document.getElementById(SelectedCategory);
         VAR SelCategory = Category.options [Category.selectedIndex] .value的;
         警报(SelCategory);     }
    < / SCRIPT>

我想获得 SelCategories.Tax SelCategories.Duty 将它们添加到权重值和总价格显示标签的总..我使用的 ASP.NET MVC 4 ,这是我的模型,我想用

 公共类CategoriesModel
    {
        公众诠释类别ID {搞定;组; }
        公共字符串类别名称{搞定;组; }
        公共小数税{搞定;组; }
        公共小数税收{搞定;组; }
        公共IEnumerable的< SelectListItem> CategoriesList {搞定;组; }
    }


解决方案

我觉得这里最好的方法是使用JSON和 Knockout.js (但你也可以不用敲,如果你的情况很简单)。

首先,你需要在PM控制台命令安装JSON支持:

  PM>安装包NewtonSoft.Json

然后,在你看来,你可以将您的模型转换为这样的JavaScript对象:

  @model ...
@using Newtonsoft.Json...<脚本类型=文/ JavaScript的>    VAR数据= @ Html.Raw(JsonConvert.SerializeObject(this.Model));< / SCRIPT>

然后你就可以访问所有的属性在模型中,在普通的JavaScript:

  VAR ID = data.CategoryID;

这就是它!如果你的逻辑是复杂的,你想使你的看法更强大的使用淘汰赛。这可能是一点点的为新手混乱,但是当你得到它,你将获得超级强大的知识,将能够简化您的视图code显著。

that's my function:

 <script> function Calculate()
     {
         var ItemPrice = document.getElementById("price");
         var weight = document.getElementById("weight");
         var SelWeight = weight.options[weight.selectedIndex].value;
         alert(SelWeight);
         var Category = document.getElementById("SelectedCategory");
         var SelCategory = Category.options[Category.selectedIndex].value;
         alert(SelCategory);

     }
    </script>

i want to get SelCategories.Tax and SelCategories.Duty to add them to weight value and total price to show the total in a label.. I'm using ASP.NET MVC 4 and this is my Model that i want to use

public class CategoriesModel
    {
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
        public decimal Duty { get; set; }
        public decimal Tax { get; set; }
        public IEnumerable<SelectListItem> CategoriesList { get; set; }
    }

解决方案

I think the best approach here is to use Json and Knockout.js (but also you can do it without knockout, if your case is simple).

First, you need to install Json support with a command in PM console:

PM> install-package NewtonSoft.Json

Then, in your view you can convert your model to javascript object like this:

@model ...
@using Newtonsoft.Json

...

<script type="text/javascript">

    var data = @Html.Raw(JsonConvert.SerializeObject(this.Model));

</script>

Then you can access all the properties in your model with in plain JavaScript:

var id = data.CategoryID;

That's it! Use knockout if your logic is complicated and you want to make your view more powerful. It could be a little bit confusing for newbie, but when you get it, you'll gain the super-powerful knowledge and will be able to simplify your view code significantly.

这篇关于如何从模型中获取的数据为JavaScript MVC 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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