Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
HI,

how to pass Ienumerable modelView to controller Actionresult.. I tried the following code

XML
[HttpPost]       
       public ActionResult PostingComment(IEnumerable<NotificationModel> vii)
       {

           return View(vii);
       }
br mode="hold" />but the code is not hitting when using in


Thanks
BS
Posted
Comments
nithibs 30-Dec-13 7:27am
   
how to get values from two models in controller
nithibs 30-Dec-13 10:47am
   
http://www.c-sharpcorner.com/UploadFile/97fc7a/return-multiple-models-in-single-view-in-mvc3/

i done the view & model as per above link.. now what i need is how to bind the values in model and return back to controller.

you can use ToList() method to pass Ienumerable to a model.


C#
return View(vii.ToList());
   
In simple terms

1. Say suppose you have a class

C#
public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
}


2. You controller could be

C#
public ActionResult Contact(Student student)
       {
           ViewBag.Message = "Your contact page.";
           ViewBag.id = student.Id;
           ViewBag.name = student.Name;

           return View(student);
       }



3. Your view could be


@model Mvc.Models.Student



YOUR ID = @Model.Id
YOUR NAME = @Model.Name
   
XML
[HttpPost]
       public ActionResult PostingComment(IEnumerable<NotificationModel> vii)
       {

           return View(vii.AsEnumerable());
       }
   
Comments
nithibs 30-Dec-13 7:26am
   
hi, i am getting null value when i am passing the model as above
can you show your code.when page postback then data bind data in model or not. can you verify this.
   
Comments
nithibs 30-Dec-13 8:49am
   
[HttpPost]
public ActionResult Notification(IEnumerable<notificationmodel> _camp)
{
return View(_camp);
}

Model

<%foreach (var item in Model)
{
for (int i = 0; i < item.Customer.Count; i++)
{ %>
<div class="accordion-heading">
<div class="clearfix">
<%
var campcnt = 0;
for (int j = 0; j < item.Campaign.Count; j++)
{
if (item.Customer[i].CustomerID == item.Campaign[j].CustCampid)
{
campcnt = campcnt + 1;
}
}
%>
<span class="pull-left">
<%:item.Customer[i].CustomerID %>
<span class="camp"><%:campcnt %> campaign</span>
</span>
<span class="label label-success pull-right">Renewed</span>
</div>
<div class="clearfix">



<%:item.Customer[i].CustomerName %>



<%:item.Customer[i].CustClearedamt %>


</div>
</div>
<
CHill60 1-Jan-14 14:08pm
   
If you have a question or comment for the OP please use the 'Have a Question or Comment' link - don't post further questions as solutions
nithibs 2-Jan-14 12:06pm
   
i doesn't posted any question or comments as solution!!
CHill60 2-Jan-14 12:08pm
   
My comment was directed at jadev suresh
can you spacified folling in view. Do you preform edit,delete opration in view. if yes then use list instead of ienurable.

@model IEnumerable Model
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900