2014-02-12 34 views
0

我已經創建了ASP.NET MVC 5.0應用程序。我的控制器包含查詢Microsoft Dynamics CRM 2013應用程序的操作。MVC - 發送MS CRM實體以強類型查看

此操作旨在檢索單個聯繫人實體。但是,當結果到達並且強類型視圖在瀏覽器中呈現時,我看不到該字段的值 - 只有屬性名稱。

使用調試器,我已確認控制器操作成功地從Microsoft CRM中檢索一行數據並填充聯繫人類並將其發送到我的視圖。

例如,當我進入即時窗口中顯示的實際值:?contact.firstname這產生的價值:「約翰」

上面的例子此刻的測試時,我的控制器返回的查詢結果爲聯繫人類(見下文)。

var contact = (MVCNursePortal.Contact)context.ContactSet.Where(c => c.ContactId == cid).FirstOrDefault();

但是發送的類(return View(contact);)後,沒有數據實際到達到,即使該模型是正確的類型(@model Contact)我的看法。

出於某種原因,我似乎沒有傳遞實際數據,或者我的視圖模型不正確。

我的命名空間中只有一個Contact模型。在控制器或視圖中,不知道我們錯誤的地方在哪裏?

運行在Internet Explorer中的項目後,該視圖只顯示以下內容:


EditContact


你意見極大的讚賞。

代碼示例如下:

控制器:

using Microsoft.Xrm.Sdk.Discovery; 
    using Microsoft.Xrm.Sdk.Messages; 
    using Microsoft.Xrm.Sdk.Metadata; 
    using MVCNursePortal.Infrastructure; 

    namespace MVCNursePortal.Controllers 
    { 
     public class HomeController : Controller 
     { 
      private IOrganizationService oMSCRMService; 

      public ActionResult EditContact(string contactID) 
      { 
       CRMFunctions fns = new CRMFunctions(); 
       oMSCRMService = fns.fn_MSCRMConnect(); 
       Guid cid = new Guid(contactID); 

       var context = new MVCNursePortal.xrm(oMSCRMService); 

       var contact = (MVCNursePortal.Contact)context.ContactSet.Where(c => c.ContactId == cid).FirstOrDefault(); 

       return View(contact); 
      } 
     } 
    } 

的觀點:

@model Contact 
    @{ 
     ViewBag.Title = "EditContact"; 
    } 
    @Html.LabelFor(c => c.FirstName) 

連接到Microsoft Dynamics CRM

using Microsoft.Xrm.Sdk.Client; 
    using Microsoft.Xrm.Sdk.Query; 
    using Microsoft.Xrm.Sdk.Discovery; 
    using Microsoft.Xrm.Sdk.Messages; 
    using Microsoft.Xrm.Sdk.Metadata; 
    using System.Configuration; 
    using System.ServiceModel.Description; 

    namespace MVCNursePortal.Infrastructure 
    { 
     public class CRMFunctions 
     { 
      private OrganizationServiceProxy oMSCRMServiceProxy; 

      private IOrganizationService oMSCRMService; 
      public IOrganizationService fn_MSCRMConnect() 
      { 

       try 
       { 
        Uri OrgURI = new  Uri(ConfigurationManager.ConnectionStrings["crmUrl"].ConnectionString); 
        Uri HomeURI = null; 
        ClientCredentials oCredentials = new ClientCredentials(); 

        //Update these to be in the web.config: 
        oCredentials.Windows.ClientCredential.Domain = System.Configuration.ConfigurationManager.AppSettings["Domain"].ToString(); 
        oCredentials.Windows.ClientCredential.UserName = System.Configuration.ConfigurationManager.AppSettings["Username"].ToString(); 
        oCredentials.Windows.ClientCredential.Password = System.Configuration.ConfigurationManager.AppSettings["Password"].ToString(); 

        oMSCRMServiceProxy = new OrganizationServiceProxy(OrgURI, HomeURI, oCredentials, null); 
        oMSCRMServiceProxy.EnableProxyTypes(); 
        oMSCRMService = (IOrganizationService)oMSCRMServiceProxy; 

        return oMSCRMService; 

       } 
       catch (Exception ex) 
       { 
        return oMSCRMService; 
       } 
      } 


     } 
    } 

的Web.Config

<?xml version="1.0" encoding="utf-8"?> 
    <!-- 
     For more information on how to configure your ASP.NET application, please visit 
     http://go.microsoft.com/fwlink/?LinkId=301880 
     --> 
    <configuration> 
     <configSections> 
     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
     <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"></section> 
     </configSections> 
     <connectionStrings> 
     <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MVCNursePortal-20140212082427.mdf;Initial Catalog=aspnet-MVCNursePortal-20140212082427;Integrated Security=True" 
      providerName="System.Data.SqlClient" /> 
     <add name="crmUrl" connectionString="http://mycrmserver/customerOrgName/XRMServices/2011/Organization.svc"/> 
     </connectionStrings> 
     <appSettings> 
     <add key="webpages:Version" value="3.0.0.0" /> 
     <add key="webpages:Enabled" value="false" /> 
     <add key="ClientValidationEnabled" value="true" /> 
     <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
     <add key="Username" value="joe"/> 
     <add key="Password" value="1234"/> 
     <add key="Domain" value="domainname.com"/> 
     </appSettings> 
     <system.web> 
     <authentication mode="None" /> 
     <compilation debug="true" targetFramework="4.5" /> 
     <httpRuntime targetFramework="4.5" /> 
     </system.web> 
     <system.webServer> 
     <modules> 
      <remove name="FormsAuthenticationModule" /> 
     </modules> 
     </system.webServer> 
     <runtime> 
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
      <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
      <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
      <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
      <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
      <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
      <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
      </dependentAssembly> 
     </assemblyBinding> 
     </runtime> 
     <entityFramework> 
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
      <parameters> 
      <parameter value="v11.0" /> 
      </parameters> 
     </defaultConnectionFactory> 
     <providers> 
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
     </providers> 
     </entityFramework> 
    </configuration> 
+0

你有解釋你的應用MVCNursePortal.xrm(oMSCRMService)部分的代碼嗎?你是如何創建聯繫人實體類和ContactSet函數來自哪裏的?我也在嘗試創建一個MVC 5 CRM應用程序,並且希望強制輸入我的數據。 – SpaceCowboy74

回答

1

你的輸出是用於您所提供的剃刀正確。 LabelFor()打印屬性的名稱(用於在輸入框旁邊打印標籤等)。要打印出名稱的值,只需使用@Model.FirstName即可。

+0

謝謝安東尼。我只是嘗試了你的建議,你是對的。我需要更多地瞭解剃刀! :) – Tim