Controller:
Private ReadOnlyVipviewmodel _model =NewVipviewmodel (); Public Static stringMSG;//Get:systemmanage/renzheng[HttpGet] PublicActionResult Index (intpage =1) { #regionData is populated to the custom modelvardb =Congfig.mysqlconn; Const stringsql ="SELECT s,w from SW"; varresult =db. Executedatareader (SQL); Mysqlconnection Con=NewMysqlconnection ("server=012.345.678.910;port=0123; User id=userid;password=pwd;database=db"); Mysqldataadapter da=Newmysqldataadapter (sql, con); DataSet DS=NewDataSet (); Da. Fill (DS); DataTable DT= ds. tables[0];//get data from another database (MYSQL)//reading from the database varCategoryList =NewList<comm2viewmodel>(); for(inti =0; i < dt. Rows.Count; i++) {Categorylist.add (NewComm2viewmodel () {Id = dt. rows[i][0]. ToString (), Name = dt. rows[i][1]. ToString ()}); } _model.comm2list= CategoryList;//Fill #endregion varPageSize = request["pagesize"] ==NULL?Ten:int. Parse (request["pagesize"]); Oqlcompare Fun (Oqlcompare cmp, Users u) {cmp= Cmp.comparer (U.isdelete,"=",false); CMP= Cmp.comparer (U.status,"=",1);//users who have applied for certification returnCMP; } varTotal = Curd.getrecordcounts ((oqlcomparefunc<users>); _model. Userslist= OQL. Fromobject<users> (). Select (). Where (Fun). (o, b) = =O.desc (b.id)). Limit (pageSize, page, total). ToList (); Viewbag.paging= Paging.pagenavigate (pageSize, page, Total,""); Viewbag.msg=MSG; MSG=""; Viewbag.deleteurl="Address"; returnView (_model); }
View Views:
@model: /. Models.vipviewmodel <!--Here is a reference to the custom model, the address is actually changed to
<Tableclass= "Table"> <thead> <TR> <th>Id</th> <th>Company Name</th> <th>Position</th> <th>Certified Name</th> <th>Contact phone</th> <th>Community</th> <th>Room number</th> <th>Add date</th> <th>Operation</th> </TR> </thead> <tbody>@if (Model.UsersList.Count > 0) { foreach (var item in model.userslist) {<TR> <TD> <P>@item. Id</P> </TD> <TD> <P>@item. CompanyName</P> </TD> <TD> <P>@item. Type</P> </TD> <TD> <P>@item. Name</P> </TD> <TD> <P>@item. Phone</P> </TD> <TD>@{foreach (Var item2 in Mo Del. Comm2list) {if (item2.i D = = Item. Community2) {
<P>@item2. Name</P>Break ; } } } </TD> <TD> <P>@item. Community</P> </TD> <TD> <P>@item. Createtime.tostring ("F")</P> </TD> <TD> <Divclass= "Operate"> <ahref=""class= "Gray">Through certification</a> <ahref= "javascript:;"class= "Red"onclick="">Reject authentication</a> </Div> </TD> </TR> } } </tbody> </Table>
Vipviewmodel: Public classVipviewmodel { PublicList<users> Userslist {Get;Set; } PublicList<comm2viewmodel> Comm2list {Get;Set; } }comm2viewmodel: Public classComm2viewmodel { Public stringId {Get;Set; } Public stringName {Get;Set; } }
Finally, attach the structure:
. NET MVC displays data from different databases (MSSQL, MySQL) with the same page