Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Binding CRM optionset value model in ASP.Net MVC portal Error: Object reference not set to an instance of an object.

$
0
0

Hi,

Hope some kind person might be able to help me with this. Know the CRM platform reasonably well as as a non developer customiser but this is my first attempt at trying to create a portal layer using ASP.Net MVC and am a relative novice with C#

I have created an employee model which has the following attributes (not all shown here)

public class EmployeeModel
{
[Key]
[Required]
public Guid Id { get; set; }
public String EmployeeId { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public IEnumerable<SelectListItem> County { get; set; }
public int CountyId { get; set; }

}

I'm using a helper method I lifted from a colleague's code (much better coder than me!) to deserialise

public Dictionary<int, string> GetGlobalOptionValues(string optionSetLogicalName)
{
Dictionary<int, string> optionSetDictionary = new Dictionary<int, string>();

if (string.IsNullOrEmpty(optionSetLogicalName))
return null;

var retrieveAttributeRequest = new RetrieveOptionSetRequest
{
Name = optionSetLogicalName
};

var retrieveAttributeResponse = (RetrieveOptionSetResponse)XrmContext.Execute(retrieveAttributeRequest);

var retrievedPicklistAttributeMetadata = (OptionSetMetadata)retrieveAttributeResponse.OptionSetMetadata;

var options = retrievedPicklistAttributeMetadata.Options; //.FirstOrDefault(o => o.Value == value);

if (options == null)
return null;

foreach (var option in options)
{
var label = option.Label.UserLocalizedLabel.Label;

if (option.Value.HasValue)
optionSetDictionary[option.Value.Value] = label;
}

return optionSetDictionary;
}

Here is my controller for this entity- my main problem is that I cannot find how to bind the CountyID (which is a global option set in CRM) to the view. I can see the optionset values inside in the County object but I can't parse them

public class EmployeesController : Controller
{

public ActionResult Index()
{
try
{
var context = new PortalContextClass().XrmContext;

EntityOptionSet eos = new EntityOptionSet();
IDictionary<int, string> xrmCounty = eos.GetGlobalOptionValues("new_county");
List<SelectListItem> County = new List<SelectListItem>();
foreach (var kvp in xrmCounty)
County.Add(new SelectListItem() { Value = kvp.Key.ToString(), Text = kvp.Value });

var employees =
from e in context.new_employeeSet

//where e.statuscode.Equals(100000000)
// Active
select new
{
Id = e.Id,
FirstName = e.new_firstname,
LastName = e.new_secondname,
EmployeeId = e.new_name,
FullName = e.new_FullName,
Address1 = e.new_address1,
Address2 = e.new_address2,
Address3 = e.new_address3,
County=e.new_County,
CountyId=e.new_County.GetHashCode(),
Email = e.new_employeeworkemail,
MobileNumber = e.new_employeemobilephone,
Landline = e.new_employeehomephone,
Latitude = e.new_latitude,
Longtitude = e.new_longtitude,

};

List<EmployeeModel> lstEmployees = new List<EmployeeModel>();

foreach (var employee in employees)
{

lstEmployees.Add(new EmployeeModel()
{

Id = employee.Id,
FirstName = employee.FirstName,
LastName = employee.LastName,
EmployeeId = employee.EmployeeId,
FullName = employee.FullName,
Address1 = employee.Address1,
Address2 = employee.Address2,
Address3 = employee.Address3,
CountyId = employee.CountyId,
County=employee.County
Email = employee.Email,
MobileNumber = employee.MobileNumber,
Landline = employee.Landline,
Latitude = employee.Latitude,
Longtitude = employee.Longtitude

});
}

return View("Index",lstEmployees);
}
catch (Exception ex)
{
//handle error
return View("Index",null);
}

FInally, and thanks for bearing with me here, my view includes:

<td>
@Html.DisplayFor(modelItem => employee.CountyId)
</td>
<td>@Html.DropDownListFor(modelItem=>employee.CountyId, employee.County, new { @class = "form-control" })

</td>

When I set a breakpoint inside the foreach block that builds the list that's returned I can see the option set values but I just can't get at them 

The error thrown is Error: Object reference not set to an instance of an object. When I remove the County field from the list that's passed back from the controller the view renders fine and shows the correct data from CRM which tells me that my portalcontext is retrieving data ok.

THanks for reading- maybe you might be able to point me in the right direction here

THanks
John


Viewing all articles
Browse latest Browse all 46379

Trending Articles