简体   繁体   中英

MVC: Controller's action parameter as nullable Guid?

In this function

    [HttpGet]
    public ActionResult Create_Summary(Guid? appointmentId)
    {
        if (appointmentId.HasValue)
        {
            var appointment = _appointmentRepository.GetAppointmentDetails(appointmentId);
            var summary = new CreateAppointmentSummary
            {
                NameOrSubject = appointment.Subject,
                Location = appointment.Location,
                DescriptionOrRemarks = appointment.Description
            };
            return View(summary);
        }
        else
        {
            var summary = new CreateAppointmentSummary();
            return View(summary);
        }

    }

I get error : Cannot convert System.Guid? to System.Guid Cannot convert System.Guid? to System.Guid while calling the repository function.

What I want to do make this action to be able to be called with some null guid value as well from some other view. But if they call with some not null guid value then call the resposiotry function to do some search.

But i'm having trouble to make this work.

What Brandon said - you need this when you call the repository function.

var appointment = _appointmentRepository.GetAppointmentDetails(appointmentId.Value);

Guid? and Guid are totally different types; GetAppointmentDetails requires Guid .

Please try :

(Guid?) new Guid(appointmentId)

just cast it: (Guid?)(new Guid(myString))

there is also an implicit cast, so this would work fine as well: Guid? g = new Guid(myString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM