简体   繁体   English

asp.net mvc c#从数据库表创建并显示自定义变量并置项

[英]asp.net mvc c# create and display custom variable concatenate items from database table

Lived in a hole after years in complex classic asp world - trying to get out ... I'm not even sure how to search properly since the syntax have changed so much, but here is my attempt at explaining my problem in classic lingo: 在复杂的经典asp世界中生活了数年-试图逃脱...由于语法已经发生了很大变化,我什至不知道如何正确搜索,但是这是我尝试解释经典lingo中的问题:

  • Existing Database / Database First 现有数据库/数据库优先

I want to display a concatenated variable on a details page. 我想在详细信息页面上显示一个串联的变量。 For example a simple concatenation of the database results for first name, last name, suffix etc. column fields. 例如,数据库结果的简单串联包括名字,姓氏,后缀等列字段。 I cannot figure out how to create a concatenated string in a ViewModel, in the controller, in the View itself, metadata (via partialClasses), and then how to call and display it in the view. 我无法弄清楚如何在ViewModel中,在控制器中,在视图本身中创建元数据字符串(通过partialClasses),然后如何在视图中调用和显示它。 Where is the best place to create this "variable" ? 创建此“变量”的最佳位置在哪里?

ps. ps。 Guidance in which search terms to use / tutorials will be appreciated as well. 还将使用其中的搜索词/教程的指南。

the model is the right place for this not the controller or the view. 模型是正确的位置,而不是控制器或视图。

in your model something like 在您的模型中

     public string ConcatenatedName
            {
                get
                {
                    return string.format("{0} {1} {2}",FirstName, LastName, Suffix) 
                }
            }

 public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value;}
        }


 public string LastName
        {
            get { return _lastName; }
            set { _lastName = value;}
        }


 public string Suffix
        {
            get { return _suffix; }
            set { _suffix = value;}
        }

then use concatenated name in your view. 然后在您的视图中使用串联名称。 Having it in the model allows you to check it in unit tests which you will not be able to easily do in a view and the controller should not be cluttered with this and is just not the right place for it 将其包含在模型中可让您在单元测试中对其进行检查,而这在视图中将无法轻松完成,并且控制器不应因此而混乱不堪,而这并不是正确的选择

EDIT : Assumption - I have made an assumption that you have separated the database and UI and are not using your db obj as your model for the views. 编辑 :假设-我假设您已经将数据库和UI分开,并且没有将db obj用作视图的模型。 I am assuming you have atleast put a middle layer in and are atleast mapping (eg automapper) from database object to viewmodel object 我假设您至少有一个中间层,并且是从数据库对象到viewmodel对象的至少映射(例如automapper)

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

相关问题 如何使用带有 ASP.NET MVC 的局部视图显示数据库表中的所有项目? - How do I display all items from a table in a database using a partial view with ASP.NET MVC? C#-如何从数据库到ASP.NET MVC中的视图显示特定记录 - C# - How to Display a specific record from a database to a view in ASP.NET MVC ASP.NET Datagrid从数据库C#中检索选择的项目,并使它们像表一样排序 - ASP.NET Datagrid retrieve choosed items from database C# and make them sorted like table 从ASP.NET MVC 2 C#中的webform创建xml - create xml from webform in asp.net mvc 2 c# ASP.NET C#如何从数据库中填充表 - ASP.NET C# How to fill table from database 如何遍历和编辑在 ASP.NET MVC C# 中选中复选框的数据库实体表中的记录 - How to loop through and edit records from database entity table that has a checkbox checked in ASP.NET MVC C# c#asp.net-搜索数据列表中的项目并显示它们 - c# asp.net - Search items in DataList and display them 使用外键表列显示数据 asp.net core mvc c# - Use foreign key table columns to display data asp.net core mvc c# 为ASP.NET MVC(C#)和LINQ-to-SQL中的视图上显示的SQL表实现过滤器? - Implementing filters for a SQL table display on a view in ASP.NET MVC (C#) and LINQ-to-SQL? 在ASP.net MVC C#中显示SQL Server表的列 - Display the columns of a SQL Server table in ASP.net MVC C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM