I have edit the properies of house using following methods Edit from Controller
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
House house = repository.GetHouseById((int)id);
if (house == null)
{
return HttpNotFound();
}
NewHouse newHouse = new NewHouse()
{
House = house,
Types = repository.getAvaiableTypes()
};
return View(newHouse);
}
public ActionResult Edit([Bind(Include = "Id,Description,Type,Price")] House house,string selectedTypeText)
{
ModelState.Clear();
house.Type = selectedTypeText;
if (ModelState.IsValid)
{
try
{
repository.UpdateHouse(house);
repository.SaveChanges();
return RedirectToAction("Index");
}
catch
{
ViewBag.exception = true;
return View(house);
}
}
return View(house);
}
I use class in ModelView NewHouse, which structure looks like this
public class NewHouse
{
public House House { get; set; }
public IEnumerable<SelectListItem> Types { get; set; }
public int SelectedTypeId { get; set; }
[Display(Name = "Typ domku")]
public string SelectedTypeText { get; set; }
}
When I save changes all fields in object House and field selectedTypeText are passed to POST method. But I have an error
The model item passed into the dictionary is of type 'Repository.Models.House', but this dictionary requires a model item of type 'Repository.ViewModels.NewHouse.
I clear ModelState, but it doesn't help.
How should I bind to pass object House and field selectedTypeText in POST method?
In Edit.cshtml I use @model Repository.ViewModels.NewHouse