Reputation: 248
I am trying to pass list of data through viewbag from controller to partial view but getting error
in login form after submitting data taking it from formcollection through HttPost and once action complete it return to home page from there i am calling method Page_Init inside that in 'loadmessage' method i am trying to return list to a partial view "Header" based on condition.but not able to perform getting error
Home controller [HttpPost]
public ActionResult Login(FormCollection form)
{
return View("Home");
}
in Home.cshtml calling method page_init in controller
$.get("/Home/Page_Init",null, function (data) {
alert(data);
});
Home controller
public ActionResult Page_Init()
{
loadMessages();
return view("Home");
}
public ActionResult loadMessages()
{
List<MessageModel> lstMessages = new List<MessageModel>();
List<MessageModel> lstInfoMessages = new List<MessageModel>();
lstInfoMessages = lstMessages.Where(msg => msg.MESSAGE_TYPE.Equals(CommonConstants.SAFETY_MESSAGE_INFO, StringComparison.InvariantCultureIgnoreCase)).ToList<MessageModel>();
if (lstInfoMessages.Count > 0)
{
ViewBag.lstInfoMessages = 1;
ViewBag.lstInfoMessages1 = lstInfoMessages;
return PartialView("Header", lstInfoMessages);
}
}
also trying to go to partial view from Home view
@ViewBag.lstInfoMessages1
@if (ViewBag.lstInfoMessages == 1)
{
@Html.Partial("Header",ViewBag.lstInfoMessages1)
}
Expected that list of information should go to partial view and bind Error:Not getting Exact syntax what to do and how to proceed the steps tried above throw error
Upvotes: 0
Views: 776
Reputation: 1153
@Html.Partial
method does not accept the dynamic value – so we need to cast it to the actual type.
@model MessageModel //you need to give correct path of MessageModel
@ViewBag.lstInfoMessages1
@if (ViewBag.lstInfoMessages == 1)
{
@Html.Partial("Header", (List<MessageModel>)ViewBag.lstInfoMessages1)
}
In Header Partial view, you can retrieve list using @Model
Upvotes: 1