How to pass data from view to controller in mvc
Hi frnds
I searched internet but didnt understand.
There is mentioned using hidden field or post method.but post method we used using Ajax.
Please clear me this and send me the code which i can explain front of the interviewer..
How many types we can pass data from view to controller ?
Mangesh GPosted Dec 12, 2016, 2:22 AM
Bikesh SrivastavaPosted Dec 12, 2016, 8:37 AM
MayankPosted Dec 12, 2016, 8:36 AM
{
var user = new Users
{
Id = 1,
UserName = "A"
};
var user2 = new Users
{
Id = 2,
UserName = "B"
};
var user3 = new Users
{
Id = 3,
UserName = "C"
};
List
list.Add(user);
list.Add(user2);
list.Add(user3);
var loc = new Location
{
Id = 1,
LocationName = "UP"
};
var loc2 = new Location
{
Id = 2,
LocationName = "Delhi"
};
var loc3 = new Location
{
Id = 3,
LocationName = "Bihar"
};
List
lst.Add(loc);
lst.Add(loc2);
lst.Add(loc3);
var model = new CheckBoxClass
{
Usr = list,
Loc = lst
};
return View(model);
}
[HttpPost]
public ActionResult CheckBoxListSelection(CheckBoxClass model)
{
//Here you will get the location and user in list u can split it by split funtion and save in DB
return RedirectToAction("CheckBoxListSelection");
}
{
public List
public List
public string LocationList { get; set; }
public string UserList { get; set; }
}
public class Users
{
public int Id { get; set; }
public string UserName { get; set; }
}
public class Location
{
public int Id { get; set; }
public string LocationName { get; set; }
}
@*~/Models/CheckBoxClass.cs*@
@{
ViewBag.Title = "CheckBoxListSelection";
}
CheckBoxListSelection
@using (Html.BeginForm("CheckBoxListSelection", "Home", FormMethod.Post))
{
@foreach (var items in Model.Usr as List
{
@items.UserName
}
@Html.HiddenFor(M => M.UserList, new { id = "userIdLst" })
@foreach (var items in Model.Loc as List
{
@items.LocationName
}
@Html.HiddenFor(M => M.LocationList, new { id = "locIdLst" })
}
Prakash KumarPosted Dec 12, 2016, 12:24 AM
Manas MohapatraPosted Dec 11, 2016, 2:14 PM