I have a querystring like //categories/details/213
Now I had to fill the page only with details of item 213. There are many textboxes in which data should be shown.
My function looks like:
public ActionResult EditCustomer(string id)
{
int cust_id;
cust_id=Convert.ToInt32(id);
var customer = (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new
{
tb.First_Name,tb.Last_Name,tb.Country_Code,tb.City,tb.State_ID}).ToList();
}
return View();
Now how do I get these data to View. Do I have to put all these in ViewBag? There are many textboxes only few are shown here. Please suggest anything.
As per Code Requirment you need to Create Model.
Like
Public Calss Customer
{
Public String FirstName{get;set;}
Public String lastName{get;set;}
Public String Country{get;set;}
Public String City{get;set;}
Public String State{get;set;}
}
public ActionResult EditCustomer(string id)
{
int cust_id;
Customer cus = new Customer(); cust_id=Convert.ToInt32(id);
List<Customer> cust = (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new
cus {
FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList();
}
return View(cust);
In View
@Model IEnumerable<Customer>
and you can use this model for fill Value.
Like
@Html.TextBox("txtname", Model.FirstOrDefault().Name);
List<Customer> cust = (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new
cus {
FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList();
Error:
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<QTicket.Controllers.CustomerController.Customer1>
But I have one question why do we have to make an seprate class. I am using Entity framework and linq. I have all my tables in entity form like tblCustomer(used above). Is there any more method to perform it Or only this one will work. Like I tried using
List<tblCustomer> cust= (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new tblCustomer
{ FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList();
abhijay
Member
1 Points
24 Posts
Post data to View
Nov 26, 2012 07:51 AM|LINK
I have a querystring like //categories/details/213
Now I had to fill the page only with details of item 213. There are many textboxes in which data should be shown.
My function looks like:
public ActionResult EditCustomer(string id)
{
int cust_id;
cust_id=Convert.ToInt32(id);
var customer = (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new
{
tb.First_Name,tb.Last_Name,tb.Country_Code,tb.City,tb.State_ID}).ToList();
}
return View();
Now how do I get these data to View. Do I have to put all these in ViewBag? There are many textboxes only few are shown here. Please suggest anything.
ignatandrei
All-Star
134750 Points
21593 Posts
Moderator
MVP
Re: Post data to View
Nov 26, 2012 07:59 AM|LINK
Via the Model.
The ViewModel is not necessary the DatabaseModel . Please see http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
abhijay
Member
1 Points
24 Posts
Re: Post data to View
Nov 26, 2012 09:06 AM|LINK
Can you send me code in rar format. Actually I cant download code in zip format.
mail is abhijay.singh@ymail.com
ashoksudani
Participant
1043 Points
213 Posts
Re: Post data to View
Nov 26, 2012 09:24 AM|LINK
As per Code Requirment you need to Create Model. Like Public Calss Customer { Public String FirstName{get;set;} Public String lastName{get;set;} Public String Country{get;set;} Public String City{get;set;} Public String State{get;set;} } public ActionResult EditCustomer(string id) { int cust_id; Customer cus = new Customer(); cust_id=Convert.ToInt32(id); List<Customer> cust = (from tb in db.tblCustomers where tb.Customer_ID == cust_id select new cus { FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList(); } return View(cust); In View @Model IEnumerable<Customer> and you can use this model for fill Value. Like @Html.TextBox("txtname", Model.FirstOrDefault().Name);abhijay
Member
1 Points
24 Posts
Re: Post data to View
Nov 26, 2012 11:02 AM|LINK
Its throwing an error in
List<Customer> cust = (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new
cus {
FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList();
Error:
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<QTicket.Controllers.CustomerController.Customer1>
CPrakash82
All-Star
18236 Points
2837 Posts
Re: Post data to View
Nov 26, 2012 11:04 AM|LINK
Change the below line.
select new customer
{ FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList();
abhijay
Member
1 Points
24 Posts
Re: Post data to View
Nov 26, 2012 11:27 AM|LINK
Thanks it works.
But I have one question why do we have to make an seprate class. I am using Entity framework and linq. I have all my tables in entity form like tblCustomer(used above). Is there any more method to perform it Or only this one will work. Like I tried using
List<tblCustomer> cust= (from tb in db.tblCustomers
where tb.Customer_ID == cust_id
select new tblCustomer
{ FirstName= tb.First_Name,LastName=tb.Last_Name,Country=tb.Country_Code,City=tb.City,State=tb.State_ID}).ToList();
But I get error in it.