?
Solved

UpdateModel will not update

Posted on 2010-08-19
2
Medium Priority
?
612 Views
Last Modified: 2013-11-08
The UpdateModel command does not update my object. The code is executed with no changes. Can you see why?
I think the problem is the name of the prefix. I have tried changing it; "Contract", "Contracts" and "contract".

The controller;
        [Authorize(Roles = "Inputter")]
        [HttpPost]
        public ActionResult Edit(int contractId, FormCollection formValues)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ITOCEntities())
                {
                    var contract = db.Contracts.Single(x => x.ContractId == contractId);
                    string letter = contract.ContractNo_Letter;

                    UpdateModel(contract, "Contracts", formValues);
                    if (contract.ContractNo_Letter != letter)
                        contract.ContractNo_Number = Contract.AutogenerateContractNumber(contract.ContractNo_Letter);

                    var clientContact = db.Contacts.Single(x => x.ContactId == contract.ClientContactId);
                    UpdateModel(clientContact, "Contact", formValues);
                    var contractAdministrationContact = 
                        db.Contacts.Single(x => x.ContactId == contract.ContractAdministratorContactId);
                    UpdateModel(contractAdministrationContact, "Contact", formValues);
                    db.SaveChanges();
                }

                return RedirectToAction("List");
            }
            return View();
        }
------------------------------------------------------------
The ViewModel

    public class ContractViewModel
    {
        //private ITOCEntities db = new ITOCEntities();
        public Contract contract { get; set; }
        public IList<ContractType> contractTypes { get; set; }
        public IEnumerable<SelectListItem> contractTypesSelectList
        {
            get
            {
                return this.contractTypes.Select(item => new SelectListItem
                {
                    Text = item.ContractType1,
                    Value = item.ContractTypeId.ToString()
                });
            }
        }

 
        public Contact clientContact { get; set; }
        public Contact contractAdministratorContact { get; set; }

        public ContractViewModel()
        {
            using (var db = new ITOCEntities())
            {
                this.contractTypes = db.ContractTypes.ToList();

                this.clientContact = new Contact();
                this.contractAdministratorContact = new Contact();
                this.clientContact.ContactTypeId =
                    db.ContactTypes.Where(x => x.ContactType1 == "Client").SingleOrDefault().ContactTypeId;
                this.contractAdministratorContact.ContactTypeId =
                    db.ContactTypes.Where(x => x.ContactType1 == "CA").SingleOrDefault().ContactTypeId;
            }
        }
        public ContractViewModel(int contractId)
        {
            using (var db = new ITOCEntities())
            {
                this.contractTypes = db.ContractTypes.ToList();
                this.contract = db.Contracts.Where(x => x.ContractId == contractId).SingleOrDefault();
                this.clientContact = 
                    db.Contacts.Where(x => x.ContactId == this.contract.ClientContactId).SingleOrDefault();
                this.contractAdministratorContact =
                    db.Contacts.Where(x => x.ContactId == this.contract.ContractAdministratorContactId).SingleOrDefault(); 
          
            }
        }
--------------------------------------------------------
The ViewModel reference in the view;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<ITOC.WebUI.Models.ContractViewModel>" %>

Open in new window

0
Comment
Question by:hijiki7777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
thaytu888888 earned 1500 total points
ID: 33477138
- UpdateModel usually doesn't working properly, may be you HTML form contains check box, combo box...etc, it only work well when the form controls are all text box, better don't use UpdateModel, try to set contract data manually
0
 

Author Closing Comment

by:hijiki7777
ID: 33486491
I can't say I am happy with this solution, but as far as I can tell you are right and as a result I took your advice and got it working.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question