简体   繁体   English

使用自动ng-bind创建ASP MVC自定义视图

[英]Creating ASP MVC custom views with automatic ng-bind

I am thinking of doing the bind coding task on the server side. 我正在考虑在服务器端执行绑定编码任务。 Let me describe. 让我来形容一下。 In ASP MVC i was making a viewmodel for each page then by taking help of @Html.EditorFor and others, I was making the view part. 在ASP MVC中,我正在为每个页面创建一个viewmodel,然后通过@ Html.EditorFor和其他人的帮助,我正在制作视图部分。 Now i want to make a logical relation between server side ViewModel and the one in the Scope of angular controller. 现在我想在服务器端ViewModel和角度控制器范围之间建立逻辑关系。 I have an idea of making a DisplayTemplate and a EditorTemplate for each type of field then I put the ng-bind attribute in them, so every thing sounds good except two: 我想为每种类型的字段制作一个DisplayTemplate和一个EditorTemplate然后我将ng-bind属性放在其中,所以除了两个之外,每个东西听起来都很好:

  1. How can I make an IScope of elements on the page automatically? 如何自动在页面上制作元素的IScope?

  2. In the templates I should use ng-model for example with a text in value. 在模板中,我应该使用ng-model,例如使用值中的文本。 I want the text be the name of the field that this template goes on its UIHint. 我希望文本是该模板在其UIHint上的字段名称。 So how to do that job? 那么该怎么做呢?

Edit 编辑

I forgot to say that I am using TypeScript on client side, the question one was all about this. 我忘了说我在客户端使用TypeScript,问题就在于此。 I want to make an interface (IPageScope) for the controller over the viewmodel used in that. 我想通过在其中使用的viewmodel为控制器创建一个接口(IPageScope)。

Second problem solved 第二个问题解决了

How to get model's field name in custom editor template 如何在自定义编辑器模板中获取模型的字段名称

I was looking for something like this: 我在寻找这样的东西:

@{ string modelName = ViewData.ModelMetadata.ContainerType.Name + "." + ViewData.ModelMetadata.PropertyName;}

<h3 ng-model="@modelName">
    Hello World @ViewData.ModelMetadata.ContainerType || @ViewData.ModelMetadata.ContainerType.Name || @ViewData.ModelMetadata.PropertyName || @ViewData.TemplateInfo.HtmlFieldPrefix
</h3>

Now the model name is generated by the class name and property name. 现在,模型名称由类名和属性名生成。

What about the first one? 第一个怎么样? is there any way to auto generate this scope parameters of ViewModel in an IScope interface in TypeScript file for further use? 有没有办法在TypeScript文件的IScope接口中自动生成ViewModel的范围参数以供进一步使用?

I solved the problem by searching the web, and I am posting my achievements whole together here for you all. 我通过搜索网络解决了这个问题,而且我在这里为你们所有人发布了我的成就。

First I replaced using the default display and editor template of any field of my view model to the ones that have ng-model attribute like the following view model: 首先,我使用视图模型的任何字段的默认显示和编辑器模板替换为具有ng-model属性的模板,如下面的视图模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Takhfifestan.Data;
using TypeLite;

namespace Takhfifestan.Service.Models
{
    [DataContract(Name = "ProductListViewModel")]
    [KnownType(typeof(ProductListViewModel))]
    [TsClass]
    public class ProductListViewModel
    {
        public long Id { get; set; }
        [UIHint("CodeNumber")]
        public string ProductCode { get; set; }

    }
}

Then I created those templates something like below: 然后我创建了以下类似的模板:

@{ string modelName = ViewData.ModelMetadata.ContainerType.Name + "." + ViewData.ModelMetadata.PropertyName;}

<h3 ng-model="@modelName">
    Hello World 
</h3>

Then I needed an IScope (that is my client side view model) and the TextTemplate was a good solution. 然后我需要一个IScope(这是我的客户端视图模型),TextTemplate是一个很好的解决方案。 but another person have done this before as perfect as possible: 但是另一个人在尽可能完美之前做到了这一点:

http://type.litesolutions.net/ http://type.litesolutions.net/

But this library needs a little change: 但是这个库需要一点改变:

<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="$(TargetDir)Takhfifestan.Service.dll" #>
<#@ assembly name="$(TargetDir)TypeLite.dll" #>
<#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>
<#@ assembly name="$(TargetDir)$(TargetFileName)" #>

<#@ import namespace="TypeLite" #> 
<#@ import namespace="TypeLite.Net4" #> 
<#@output extension=".d.ts"#>

 <#@include file="Manager.ttinclude"#>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>

<# var ts = TypeScript.Definitions()
        .WithReference("Enums.ts")
        .ForLoadedAssemblies()
        .WithTypeFormatter((type, f) => "I" + ((TypeLite.TsModels.TsClass)type).Name + "Scope");
#>

<#= ts.Generate(TsGeneratorOutput.Properties) #>

<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>

Now the IScope interfaace is something like this: 现在IScope interfaace是这样的:

declare module Takhfifestan.Service.Models {
    interface IProductListViewModelScope {
        Id: number;
        ProductCode: string;
    }
}

Now just think about the page! 现在只想想这个页面! every thing sounds very well. 每件事听起来都很好。 I will put the elements by Html helper and they will contain appropriate ng-model or any binding stuff. 我将通过Html帮助器放置元素,它们将包含适当的ng-model或任何绑定的东西。 and the IScope interface that I use in the controller input will be a new interface that just needs to implement the generated "IProductListViewModelScope" and now I can say that much of the work is done! 我在控制器输入中使用的IScope接口将是一个新的接口,只需要实现生成的“IProductListViewModelScope”,现在我可以说大部分工作已经完成!

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

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