在MVC 5视图中显示GUID

本文关键字:显示 GUID 视图 MVC | 更新日期: 2023-09-27 18:19:06

因此,我按照http://holsson.wordpress.com/2011/08/24/microsoft-dynamics-crm-2011-online-integration-getting-started-late-bound/的示例尝试创建一个使用CRM作为后台数据的MVC 5站点。我创建了一个CRMAccount Controller(不能创建一个叫做Account的,因为它被Identity系统使用了)。

我能够成功地查询CRM系统,但是当我尝试在我的视图中使用下面的Razor代码时,我得到了名称,但accountid下没有显示任何内容。accountid返回的项是GUID,如果我尝试将其设置为字符串,DisplayFor抱怨它无法渲染。

@model IEnumerable<Microsoft.Xrm.Sdk.Entity>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
    </tr>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item["accountid"])
        </td>
        <td>
            @Html.DisplayFor(modelItem => item["name"])
        </td>
    </tr>
}
</table>

我现在这样做只是为了概念验证。最后,我想把这两个放在一起作为一个下拉框,但如果我不能让GUID正确拉,我就会有问题。

编辑 :-------------------------------------------------------------------

我被要求发布控制器代码,虽然类似的东西是在我上面链接的例子。

这是我的控制器中使用的代码。

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
namespace ExternalEntities.Controllers
{
    public class CRMAccountController : Controller
    {
        // GET: CRMAccount
        public ActionResult Index()
        {
            var service = Session["ServiceProxy"] as IOrganizationService;
            if (service != null)
            {
                var context = new OrganizationServiceContext(service);
                IQueryable<Entity> query = from e in context.CreateQuery("account") select e;
                List<Entity> accts = query.ToList();
                return View(accts);
            }
            return RedirectToAction("Login", "Account");
        }
    }
}

在MVC 5视图中显示GUID

try

@Html.DisplayFor(modelItem => item.accountid)
不是

@Html.DisplayFor(modelItem => item["accountid"])

为智能感应,也请显示控制器。

编辑后的一些评论来回:

也许你有一个正在路上的DisplayTemplate ?试着做@item["accountid"],看看它是否仍然空白。(不使用DisplayFor)