views:

30

answers:

1

Hi, I want to know the working of UpdateModel() method. I just know about it is for update the current model data. but how it works exactly ? Because while I am using the UpdateModel() in edit controller method, there is functionality of file uploads. I am keeping the path of uploaded file in my db. but after executes the UpdateModel method value of path is replaces with "System.Web.HttpPostedFileWrapper" . why this should happens : Code:

 if (!String.IsNullOrEmpty(Request.Files["TDSCertificatePath"].FileName))
                {
                    TrustTrusteeMapping objTrustTrusteeMapping = trust_trustee_mapping_management.GetTrustTrusteeMappingById(objTDSDetail.TrustTrusteeMappingId);
                    string TrustTrusteeMappingName = objTrustTrusteeMapping.Trust.TrustName + "_" + objTrustTrusteeMapping.TrusteeMaster.FullName;
                    HttpPostedFileBase fileToUpload = Request.Files["TDSCertificatePath"];
                    objTDSDetail.TDSCertificatePath = CommonFunctions.UploadFile("TDSCertificatePath", "Content\\TDSCertificate\\", TrustTrusteeMappingName, fileToUpload);
                    fileToUpload = null;
                    objTrustTrusteeMapping = null;
                }

                UpdateModel(objTDSDetail);//After executes this the value of objTDSDetail.TDSCertificatePath changes as I said before.
+1  A: 

Why are you bothering with this method. Using a view model passed as action argument is so much easier:

public class MyViewModel
{
    public int TrustTrusteeMappingId { get; set; }
    public HttpPostedFileBase TDSCertificatePath { get; set; }
}

And in your action method:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // use the model here whose properties are bound from the POST request
    if (model.TDSCertificatePath.ContentLength > 0)
    {
        // a TDSCertificatePath was provided => handle it here
    }
    return RedirectToAction("success");
}
Darin Dimitrov