自动更新基于模型的视图

时间:2022-11-24 19:25:09

So I've been playing with the MVC website in VS 2013. I'm new to the concept of MVC and am trying to wrap my head around it. I have been following this turtorial

所以我一直在玩VS 2013中的MVC网站。我是MVC概念的新手,我正试图绕过它。我一直在关注这个turtorial

What I am trying to understand specifically here is - Is it possible to have the view update itself dynamically based upon Attributes assigned to properties in the model. Take the following for example:

我在这里要特别理解的是 - 是否可以根据分配给模型中属性的属性动态更新视图。以下面的例子为例:

  public class ExternalLoginConfirmationViewModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Full Name")]
    public string Fullname { get; set; }

}

No in this class we have 2 properties UserName and Fullname. Both are marked with required and they have display attributes set.

在这个类中,我们有2个属性UserName和Fullname。两者都标有必需,并且它们具有显示属性集。

And here is the view.

这是观点。

<li> @Html.LabelFor(m => m.FullName) @Html.TextBoxFor(m => m.FullName) </li> <li> @Html.LabelFor(m => m.UserName) @Html.TextBoxFor(m => m.UserName) </li>

  • @ Html.LabelFor(m => m.FullName)@ Html.TextBoxFor(m => m.FullName)
  • @ Html.LabelFor(m => m.FullName)@ Html.TextBoxFor(m => m.FullName)

  • @ Html.LabelFor(m => m.UserName)@ Html.TextBoxFor (m => m.UserName)

  • @ Html.LabelFor(m => m.UserName)@ Html.TextBoxFor(m => m.UserName)

    So is it possible to use the attributes on the model properties to auto-magically create the view so I dont have to update 3 locations everytime I change the model?

    那么是否可以使用模型属性上的属性自动神奇地创建视图,这样每次更改模型时我都不必更新3个位置?

    Thanks for the thoughts!

    谢谢你的想法!

    2 个解决方案

    #1


    0  

    No, ASP.NET MVC doesn't do this. It's meant for applications where you want views that are highly customized.

    不,ASP.NET MVC不会这样做。它适用于需要高度自定义视图的应用程序。

    The term you're looking for is Scaffolding. There's a not-very-well-supported project called MvcScaffolding, but the real deal for scaffolding in .NET is ASP.NET Dynamic Data, which I suggest you have a look at if you just need to throw together a really quick CRUD UI and don't care all that much how it looks.

    你正在寻找的术语是脚手架。有一个名为MvcScaffolding的项目支持得不是很好,但.NET中脚手架的真正优势是ASP.NET动态数据,我建议你看看你是否需要将一个非常快速的CRUD UI和不要太在意它的外观。

    #2


    0  

    If you change the public interface of your model, you will have to correct the code everywhere where it is used. So if you decide to rename the public property FullName to CompleteName You'll have update your code or it will not compile. If the code is embedded in a view, it will build but crash at runtime.

    如果更改模型的公共接口,则必须在使用它的任何位置更正代码。因此,如果您决定将公共属性FullName重命名为CompleteName,您将更新代码,否则将无法编译。如果代码嵌入在视图中,它将构建但在运行时崩溃。

    #1


    0  

    No, ASP.NET MVC doesn't do this. It's meant for applications where you want views that are highly customized.

    不,ASP.NET MVC不会这样做。它适用于需要高度自定义视图的应用程序。

    The term you're looking for is Scaffolding. There's a not-very-well-supported project called MvcScaffolding, but the real deal for scaffolding in .NET is ASP.NET Dynamic Data, which I suggest you have a look at if you just need to throw together a really quick CRUD UI and don't care all that much how it looks.

    你正在寻找的术语是脚手架。有一个名为MvcScaffolding的项目支持得不是很好,但.NET中脚手架的真正优势是ASP.NET动态数据,我建议你看看你是否需要将一个非常快速的CRUD UI和不要太在意它的外观。

    #2


    0  

    If you change the public interface of your model, you will have to correct the code everywhere where it is used. So if you decide to rename the public property FullName to CompleteName You'll have update your code or it will not compile. If the code is embedded in a view, it will build but crash at runtime.

    如果更改模型的公共接口,则必须在使用它的任何位置更正代码。因此,如果您决定将公共属性FullName重命名为CompleteName,您将更新代码,否则将无法编译。如果代码嵌入在视图中,它将构建但在运行时崩溃。