PDA

View Full Version : نمایش چند object Model در View



noroozifar
پنج شنبه 07 فروردین 1393, 02:09 صبح
سلام
دارم mvc را یاد میگیرم و بیشتر از فیلم ها استفاده کرده ام تا پی دی اف حالا سئوال اینه به چه طریق میشه چند مدل را به سمت ویو فرستاد مثلا الان یک کنترل مربوط به ایندکس که یک مدل را میفرسته به سمت ویو :
public ActionResult Index(string searchTerm=null,int page=1)
{


var model = (from r in _db.Restaurants
orderby r.Name ascending
where r.Name.StartsWith(searchTerm) || searchTerm==null
select r).ToPagedList(page,10);


return View(model);
}

و این هم ویو آن هستش :
@model IEnumerable<OdeToFood.Models.Restaurant>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@if(User.IsInRole("Admin")){
@Html.ActionLink("Create New", "Create")}
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.city)
</th>
<th>
@Html.DisplayNameFor(model => model.country)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.city)
</td>
<td>
@Html.DisplayFor(modelItem => item.country)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Reviews", "Index","Reviews", new { id=item.id },null) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}

</table>



حالا اگر ما دو تا مدل داشته باشیم به این صورت ذیل باید به چه صورت بفرستیم به ویو و به چه صورت در ویو نمایش دهیم :
public ActionResult Index(string searchTerm=null,int page=1)
{


var model1 = (from r in _db.Restaurants
orderby r.Name ascending
where r.Name.StartsWith(searchTerm) || searchTerm==null
select r).ToPagedList(page,10);

var model2 = (from r in _db.Restaurants
orderby r.Name ascending

select r).ToPagedList(page, 10);
return View(؟);
}

noroozifar
پنج شنبه 07 فروردین 1393, 09:12 صبح
کسی نمی تونه بگه این جریانش چیه ؟

ali_autumnal
پنج شنبه 07 فروردین 1393, 12:10 عصر
به این صورت




public class A
{
public int id { set; get; }
public string Name { set; get; }
}
public class C
{
public string Type { set; get; }
}
public class D
{
public int id { set; get; }
public string TypeName { set; get; }
}
public class Main
{
public IList<A> A { set; get; }
public C C { set; get; }
public IList<D> D { set; get; }
}


کلاس Main رو با مقدایر مورد نیاز پر کن و به View ارسال کن
سپس در View از PartialView های دیگه ای استفاده کن و به شکل زیر به مدل رو به اون Partial پاس بده




@Html.Partial("_Profile_RightColPartial",Model.A)
@Html.Partial("_Profile_RightColPartial",Model.D)

noroozifar
پنج شنبه 07 فروردین 1393, 15:43 عصر
اگر امکان داره یک نمونه پروژه کوچک تو وبژوال درست کن برام بزار ("ببخشید پروئی نباشه ") ممنونم