shmow
shmow

Reputation: 641

Submitting form doesn't bring over all the data

I'm trying to add an object using a PartialView inside a popup. It's a simple Rental application for which the data model was generated Model First through Entity Framework. The Controllers and Views have mostly been Scaffolded by EF. The relationship between RentalApplication and RentalObject is 1 to many, meaning a RentalObject always has to have 1 RentalApplication.

My controller looks like this:

// GET: /Calendar/Add/1
// Create a PartialView using a RentalObject as the model. 
// Use the provided ID to lock in the RentalApplication.
[HttpGet]
public PartialViewResult Add(int id)
{
    return PartialView(
        new RentalObject(db.RentalApplicationSet.FirstOrDefault(x => x.Id == id)));
}

// POST: /Calendar/Add
// Save the submitted RentalObject to the db
[HttpPost]
public ActionResult Add(RentalObject rentalobject)
{
    if (ModelState.IsValid)
    {
        try
        {
            db.RentalObjectSet.Add(rentalobject);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
    return View();
}

My object looks like this:

public partial class RentalObject
{
    public RentalObject()
    {
        this.Lease = new HashSet<Lease>();
    }

    public RentalObject(RentalApplication rentapp)
    {
        this.Lease = new HashSet<Lease>();
        RentalApplication = rentapp;
        PricePerHour = RentalApplication.DefaultPricePerHour;
        Currency = RentalApplication.DefaultCurrency;
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsAvailable { get; set; }
    public string Illustration { get; set; }
    public string Description { get; set; }
    public decimal PricePerHour { get; set; }
    public string Currency { get; set; }

    public virtual ICollection<Lease> Lease { get; set; }
    public virtual RentalApplication RentalApplication { get; set; }
}

So when I'm opening the popup (using @Ajax.ActionLink to GET the first controller Add action) I'm creating a RentalObject WITH a RentalApplication (2nd constructor) to use as the model. This works so far, the popup dialog shows the values PricePerHour and Currency from the RentalApplication.

However, when I submit the form in my PartialView popup everything gets copied over BUT the RentalApplication object. It somehow ends up creating a new RentalObject object using the PricePerHour and Currency from the original RentalApplication, but doesn't include the object itself under the RentalApplication property. My debugger even goes to the first constructor for RentalObject.

So I guess it's having trouble keeping a complex object inside another object when submitted from controller to view (GET) and back to controller (POST). Is this just poor practice on my part? Should I be using a ViewModel?

Upvotes: 1

Views: 65

Answers (1)

Charles
Charles

Reputation: 26

In the past I've had to use @Html.HiddenFor(m=>m.yourObjectHere) on objects that were not changed in the form to keep them from getting new-ed up again. I did this for every object I didn't use in the form (about 2 or 3).

Hope this helps.

Upvotes: 1

Related Questions