0

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

  • Clearing ModelState won't help and btw you have already posted the solution. Have a close look on 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.` What you have sent to **View** is `NewHouse` model but you expect it to return you `House` model which is technically not possible. Change `[Bind(Include = "Id,Description,Type,Price")] House house` to `[Bind(Include = "Id,Description,Type,Price")] NewHouse house` and explicity assign NewHouse property values to House object. – Suprabhat Biswal Aug 04 '17 at 13:39
  • In this situation all fields in NewHouse I have null,i I used @Html.HiddenFor and it doesn't help. –  Aug 04 '17 at 14:22
  • Is better solution put filelds into House class and use them as NotMapped in DataAnnotations? –  Aug 04 '17 at 14:26

1 Answers1

0

As @Prabhat already mentioned, you need to change

[Bind(Include = "Id,Description,Type,Price")] House house 

to

[Bind(Include = "Id,Description,Type,Price")] NewHouse house

Furthermore, you should change the names of your input fields in the view from e.g.

<input ... name="Description" ...> 

to

<input ... name="House.Description" ...>
rst
  • 2,510
  • 4
  • 21
  • 47