Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have developed items screen, i created view with two tables in database and retrieve values from view to update the values.

But it makes an error:
The model item passed into the dictionary is of type telecom.models.itemrates, but this dictionary requires a model item of type telecom.models.vw_items

What I have tried:

Model:
public class Vw_Items
{
public int Id { get; set; }
public string RateCode { get; set; }
public string ItemCode { get; set; }
[DisplayName("Item Name")]
public string ItemName { get; set; }
[DisplayName("Item Type")]
public string ItemType { get; set; }
public string UOM { get; set; }
[Required]
[DisplayName("Unit Price")]
public decimal? UnitPrice { get; set; }
}
Controller:

[HttpPost]
public ActionResult Edit(ItemRates itemrates)
{
if (ModelState.IsValid)
{
db.Entry(itemrates).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(itemrates);
}
View:
@model Telecom.Models.Vw_Items
Posted
Updated 17-Jul-16 21:44pm
v2
Comments
F-ES Sitecore 18-Jul-16 4:50am    
The view wants a model of type Vw_Items

@model Telecom.Models.Vw_Items

You are passing itemrates which is of type ItemRates.

return View(itemrates);

That's the issue, we don't know enough about your solution to offer an answer though, you haven't explained what Vw_Items is or its relation to ItemRates, we can't see the code in your view etc etc.

1 solution

Quote:
The model item passed into the dictionary is of type telecom.models.itemrates, but this dictionary requires a model item of type telecom.models.vw_items

straight forward issue, change the parameter type in the Edit action
C#
[HttpPost]
public ActionResult Edit(Vw_Items  item)
{
 
Share this answer
 
Comments
Vivek.anand34 18-Jul-16 4:20am    
thank you. but this error: System.Data.SqlClient.SqlException: View or function 'dbo.Vw_Items' is not updatable because the modification affects multiple base tables.
Vivek.anand34 18-Jul-16 4:23am    
I think its take all columns to update.. but i want to update 'ItemCode' and 'Unit Price' that is the prob. i think. And All fields are display fields ItemCode and Unit Price only textbox field
Vivek.anand34 18-Jul-16 5:17am    
How to resolve this problem.. anyone tell idea please..
Karthik_Mahalingam 18-Jul-16 6:09am    
Is there any relation in the tables
Vivek.anand34 18-Jul-16 6:00am    
Ya i change code little bit now executed:
[HttpPost]
public ActionResult Edit(Vw_Items itemrates)
{
if (ModelState.IsValid)
{
var currentPerson = db.ItemRates.FirstOrDefault(p => p.Id == itemrates.Id);
currentPerson.UnitPrice = itemrates.UnitPrice;
currentPerson.GetDate = DateTime.Now;
currentPerson.GetUser = Session["LogedUser"].ToString();
db.Entry(currentPerson).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(itemrates);
}

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