我試圖從多個模型獲取數據到我的視圖,但每次出現錯誤時,我都嘗試了很多來解決此問題,但是我失敗了... plz幫助我this..i我張貼我code..n我創建視圖模型,這個多個模型綁定(MVC 4)
這是一個索引視圖
@model Stubify.Models.ViewModels.TimeLine
<div class="media social-box social-composer ">
<a class="pull-left social-users-avatars" href="#">
<img class="media-object" width="55" height="55" src="../Content/assets/img/avatar-55.png" alt="Julio Marquez">
</a>
<div class="media-body social-body">
@foreach (var item in Model.User.Take(1))
{
<textarea id="new_message" name="new_message" class="textarea span12" placeholder="What's happening, @item.FullName?" cols="10"></textarea>
}
<div class="social-footer">
<div class="btn-group">
<a class="btn" data-original-title=""><i class="icon-map-marker"></i></a>
<a class="btn" data-original-title=""><i class="icon-camera"></i></a>
</div>
<button class="btn btn-primary pull-right" type="button">Post</button>
</div>
</div>
</div>
<div id="NewsFeeds">
@Html.Partial("_NewsFeeds", Model)
</div>
<div id="loading"></div>
這是一個視圖模型
public class TimeLine
{
public virtual IEnumerable<Users> User { get; set; }
public virtual IEnumerable<UsersPosts> UsersPost { get; set; }
}
這是一個局部視圖
@model Stubify.Models.ViewModels.TimeLine
@foreach(var item in Model)
{
<div class="social-share-image">
<img src="../Content/assets/img/gallery/sunset.jpg" alt="Sun Set">
<div class="social-share-image-text">
<h4>@item.UserId</h4>
<p><strong>@item.PostContent!</strong><small> @item.Date <i class="icon-globe"></i></small></p>
</div>
</div>
}
這是一個控制器
public ActionResult Index(string searchTerm = null, int? id = 0)
{
var viewModel = new TimeLine();
var page = id ?? 0;
viewModel.User = db.Users.OrderByDescending(u => u.Id)
.Where(u => searchTerm == null || u.FullName.ToLower().Contains(searchTerm)).Take(records);
if (Request.IsAjaxRequest())
{
return PartialView("_NewsFeeds", GetPaginatedProducts(page));
}
return View(viewModel);
}
請幫我plzzz
什麼錯誤/症狀沒有你?只是陳述_「但我失敗了」,這並不是一個好問題。 –
@HenkHolterman我想從兩個模型獲取數據到單個視圖..錯誤是「對象引用未設置爲對象的實例」。在部分視圖的循環中,如果我刪除局部視圖,代碼將正確運行,但不會渲染局部視圖 – Anony