2014-02-12 33 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