2010-10-04 62 views
0

我想知道UpdateModel()方法的工作原理。我只知道它是爲了更新當前的模型數據。但它是如何工作的?因爲當我在編輯控制器方法中使用UpdateModel()時,有文件上傳的功能。我保持上傳文件的路徑在我的數據庫。但在執行後,路徑的UpdateModel方法值將替換爲「System.Web.HttpPostedFileWrapper」。爲什麼會這樣發生的: 代碼:UpdateModel方法如何在asp.net mvc中工作2

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. 
+0

你爲什麼不搶MVC源,和調試自己呢?或者,如果你不能打擾,你可以看看它在這裏:http://aspnet.codeplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266451 – RPM1984 2010-10-04 11:39:55

+0

對不起,我試過了,但如何UpdateModel ()工作不會(顯示)內部方法執行。我希望看到。想要了解它的工作方式。 – 2010-10-04 11:50:16

+0

這是因爲'UpdateModel'最後使用了一個接口而不是一個具體的類 - 嘗試查看'DefaultModelBinder.cs'類。 – Buildstarted 2010-10-04 14:11:23

回答

1

你爲什麼用這種方法困擾。使用作爲動作參數傳遞的視圖模型是如此容易得多:

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

而在你的操作方法:

[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"); 
} 
相關問題