如何在mvc中从多个表中选择数据。 [英] How to select data from multiple table in mvc.
本文介绍了如何在mvc中从多个表中选择数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@foreach(Model.Tables中的DataRow行[tbInsertMobile]。行)
{
< tr>
@ *< td> @(row [MobileID])
< input type =textid =Txtmidstyle =display:none/>
< / td> * @
< td> @(row [MobileName])
< input type =textid =Txtmobstyle =display:无/>
< / td>
< td> @(row [MobileIMEno])
< input type =textid =TxtMEnostyle =display:none; />
< / td>
< td> @(row [mobileprice])
< input type =textid =Txtpricestyle =display:none/>
< / td>
< td> @(row [mobileManufacured])
< input type =textid =TxtManufacuredstyle =display:none/>
< / td>
< td>< input type =buttonvalue =编辑id =btneditclass =adt/>< / td>
< td>
< input type =buttonvalue =updateid =btnupdate/>
< / td>
< td>
< input type =buttonvalue =删除id =btndelete/>
< / td>
< / tr>
}
我尝试过:
如何从mvc中的多个表中选择数据。
解决方案
根据您从表中获取的数据创建包含属性的视图模型。
创建一个视图模型以绑定视图中的数据。
公共类YourViewModel
{
公共字符串Prop1 {get; set;}
公共字符串Prop2 {get; set;}
公共字符串Prop3 {get; set;}
公共字符串Prop4 {get; set;}
}
控制器
---------------
Public ActionResult()
{
YourViewModel modl = new YourViewModel();
'''用于将数据绑定到此模型中的逻辑数据库。
返回视图(YourViewName,Model);
}
查看
---------
@model列表< yourviewmodel>
$ / b $ b
@foreach(型号中的var项目)
{
}
Prop1 Prop2 Prop3 Prop4 次> @ item.Prop1 @ item.Prop2 @ item.Prop3 < TD> @ item.Prop4
@foreach (DataRow row in Model.Tables["tbInsertMobile"].Rows) { <tr> @* <td>@(row["MobileID"]) <input type="text" id="Txtmid" style="display:none"/> </td>*@ <td>@(row["MobileName"]) <input type="text" id="Txtmob" style="display:none"/> </td> <td>@(row["MobileIMEno"]) <input type="text" id="TxtMEno" style="display:none;" /> </td> <td>@(row["mobileprice"]) <input type="text" id="Txtprice" style="display:none" /> </td> <td>@(row["mobileManufacured"]) <input type="text" id="TxtManufacured" style="display:none" /> </td> <td><input type="button" value="Edit" id="btnedit" class="adt" /></td> <td> <input type="button" value="update" id="btnupdate" /> </td> <td> <input type="button" value="Delete" id="btndelete" /> </td> </tr> }
What I have tried:
How to select data from multiple table in mvc.
解决方案
Crate a view model which contains properties as per the data you fetch from the table.
Create one view model to bind the data in view.
Public Class YourViewModel
{
Public string Prop1 {get;set;}
Public string Prop2 {get;set;}
Public string Prop3 {get;set;}
Public string Prop4 {get;set;}
}
Controller
---------------
Public ActionResult()
{
YourViewModel modl= new YourViewModel();
''' Yoor logic to bind the data into this model form DB.
return view ("YourViewName", Model);
}
View
---------
@model List<yourviewmodel>
@foreach(var item in Model)
{
}
Prop1 Prop2 Prop3 Prop4 @item.Prop1 @item.Prop2 @item.Prop3 @item.Prop4
这篇关于如何在mvc中从多个表中选择数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文