Question

when updating ViewModels via

[HttpPost]
public ActionResult Edit(int id, AddressChooserEnum addressChooser, string btnSubmit, FormCollection fc)
{
    var vm = new AddressViewModel(id, addressChooser, bool.FalseString);
    vm.Address = this.repository.GetAddressById(id, addressChooser);
    try
    {
        UpdateModel(vm);
        this.repository.Save();
        return RedirectToAction("Edit", "Customer", new { id = id });
    }
    catch (Exception e)
    {
        return View(vm);
    }
}

everything works fine. But I can't get creating to work with

[HttpPost]
public ActionResult Create(int id, AddressChooserEnum addressChooser, string btnSubmit, FormCollection fc)
{
    var vm = new AddressViewModel(id, addressChooser, bool.TrueString);
    this.repository.AddAddress(); //What here? It's the simple Add method which needs an Address object.
    this.repository.Save();
    return View(vm);
}

I'm trying to create an AddressViewModel with an "empty" Address. But how can I store it with the FormCollection? Can I somehow bind the FormCollection to a new Address object? Then adding should work. Thanks in advance.

Was it helpful?

Solution

I wouldn't use the form collection. I would just try to bind straight to the View model. Without seeing your View it is hard to tell you what your actions should look like. Check out this article on Model Binding. It should help! Model Binding

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top