MVC - Sending MS CRM Entity to Strongly Typed View
我创建了一个 ASP.NET MVC 5.0 应用程序。我的控制器包含一个查询 Microsoft Dynamics CRM 2013 应用程序的操作。
该操作旨在检索联系人实体的单行。但是,当结果到达并在浏览器中呈现强类型视图时,我看不到该字段的值——只有属性名称。
使用调试器,我已确认控制器操作成功地从 Microsoft CRM 检索一行数据并填充联系人类并将其发送到我的视图。
例如,当我输入时,即时窗口会显示一个实际值:
上面的例子在我的控制器将查询结果返回到联系人类的那一刻进行了测试(见下文)。
但是在发送类 (
由于某种原因,我似乎没有传递实际数据,或者我的视图模型不正确。
我的命名空间中只有一个联系人模型。不确定到底哪里出了问题——在控制器或视图中?
在 Internet Explorer 中运行项目后,视图仅显示以下内容:
编辑联系
名字
非常感谢您的评论。
代码示例如下:
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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); } } } |
观点:
1 2 3 4 5 | @model Contact @{ ViewBag.Title ="EditContact"; } @Html.LabelFor(c => c.FirstName) |
连接到 Microsoft Dynamics CRM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <?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" /> </connectionStrings> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.webServer> <modules> <remove name="FormsAuthenticationModule" /> </modules> </system.webServer> <runtime> <dependentAssembly> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> <dependentAssembly> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <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> |
对于您提供的 Razor,您的输出是正确的。